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'];
}
}