0

I have a normal php file and my main program code is above several functions below it. I included my connect file at the top only once. Some of my custom made functions below have queries, and these functions get called in the program above. There is something which I do not understand: If I do not include my connect file in a function the query in the function won't work, if I do it does. So why do I need to include my connect file within each function?

I explain below:

This does not work throws boolean mysqli_fetch and query errors even though I connected above in the file outside the function. Why does it not work?

function queryProfileInfo($iduser){
$iduser=$_SESSION['logged_in']['iduser'];
$query ="SELECT fnlname, username, gender, email, emailconfirmed, bio, avatar, followercount, followingcount, privatepublic FROM profile Where iduser='$iduser' ";
$response=mysqli_query($dbc, $query);
    while($row =mysqli_fetch_array($response)){

                    $username= $row['username'];
}
}

/*The next example works but throws the following errors because I already connected above in the file

Notice: Constant DB_USER already defined in C:\xampp\htdocs\theproject\connect.php on line 8

Notice: Constant DB_PASSWORD already defined in C:\xampp\htdocs\theproject\connect.php on line 9

Notice: Constant DB_HOST already defined in C:\xampp\htdocs\theproject\connect.php on line 10

Notice: Constant DB_NAME already defined in C:\xampp\htdocs\theproject\connect.php on line 11

*/

    function queryProfileInfo($iduser){
include('connect.php');
    $iduser=$_SESSION['logged_in']['iduser'];
    $query ="SELECT fnlname, username, gender, email, emailconfirmed, bio, avatar, followercount, followingcount, privatepublic FROM profile Where iduser='$iduser' ";
    $response=mysqli_query($dbc, $query);
        while($row =mysqli_fetch_array($response)){

                        $username= $row['username'];
    }
    }
  • 1
    the $dbc variable needs to be declared as a global in order for you to access it from within another function. – hofan41 Mar 31 '15 at 17:22
  • 1
    ***never declare variables as global.*** – Dan Smith Mar 31 '15 at 17:23
  • So this is just a PHP rule, that I have to re-include the connection or variable $dcb in each function? Do I then just suppress the Constant errors with the @ in example 2? I'm definitely not doing global variables. –  Mar 31 '15 at 17:29

1 Answers1

0

You need to pass the variable $dbc to each function that uses it, or declare that you are using a global variable by writing global $dbc; in each function. The latter solution is not recommended, since later on you may want to have more than one database connection.

See this manual page: http://php.net/manual/en/language.variables.scope.php

Krzysztof Kosiński
  • 4,062
  • 1
  • 16
  • 22
  • Please don't recommend declaring variables as global, especially to new programmers. – Dan Smith Mar 31 '15 at 17:24
  • So this is just a PHP rule, that I have to re-include the connection or variable $dcb in each function? Do I then just suppress the Constant errors with the @ in example 2? –  Mar 31 '15 at 17:28
  • No, you do not need to include connect.php in every function. You just need to reuse the `$dbc` that you already have. If you don't pass it as a parameter, PHP will just assume that you wanted a new blank variable called `$dbc`, completely unrelated to the `$dbc` outside the function. – Krzysztof Kosiński Mar 31 '15 at 17:37