I have a connect.php file which is supposed to verify the successful connection to my database. I then have a db.php file in which I've imported the connect.php file using the require('connect.php'); function in PHP. My IDE however still gives me an error saying that the $conn variable (as defined in the code below) is not defined. At first I thought that the import of connect.php into the db.php file may have somehow failed but this seems to not be the case since launching db.php file in the browser returns "Database connection success" which should not be possible unless the connect.php file has been correctly imported.
This is the content of the connect.php file :
<?php
$host = 'localhost';
$usr = 'root';
$passwd = '';
$db_name = 'testdb';
$conn = new MySQLi($host, $usr, $passwd, $db_name);
if($conn->connect_error){
die('Database connection failure: ' . $conn->connect_error);
} else{
echo "Database connection success.";
}
I then try to import this code into the db.php as seen below :
<?php
require('connect.php');
$sql = "SELECT * FROM users";
$stmt = $conn->prepare($sql);
However in db.php the $conn variable is undefined for some reason. Why is it undefined despite the import of it's definition being successful?