Accessing method (beginner)

Asked

Viewed 27 times

-1

I started my studies recently and a question arose regarding the access of information of a method. I have an X.php file that has the following structure:

public function selectDB($sql,$params=null,$class=null){
    $query=$this->connect()->prepare($sql);
    $query->execute($params);

    $total_q = $query->rowCount();


    if(isset($class)){
        $rs = $query->fetchAll(PDO::FETCH_CLASS,$class) or die(print_r($query->errorInfo(), true));
    }else{
        $rs = $query->fetchAll(PDO::FETCH_OBJ) or die(print_r($query->errorInfo(), true));
    }
    self::__destruct();
    return $rs;
} 

And I have a Y.php page that makes use of this function. How do I make so that on the Y.php page the value of the variable is displayed $total_q ?

Could someone give me a light please?

1 answer

-1

You can call the X.php file in Y.php the way you will use it depends on how you plan to use the X.php file. May use the following: include 'X.php'; require 'X.php'; require_once 'X.php';

The choice of method to use is yours but basically refers to the error handling, in include it only displays a Warning and continues the execution of the script, no require it for execution if there is an error in the code

  • In case I call you with a include. The problem, is that when I order to echo the variable, it shows nothing. But if I am in X.php and do echo, the result appears normally.

  • Sorry about the glitch, I didn’t notice something. The variable $total_q is inside a function or it is in local scope, (only exists inside the function). Put $total_q as return of the selectDB function as well, recommend using an array

  • William, thank you very much. I declared it as a global variable, and it worked. Thank you very much. Abs,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.