I adopted code given in this example: PHP database connection class
But i changed the code with the link above and it is as follows:
class Database {
public function connect() {
define('DB_HOST', 'localhost');
define('DB_NAME', 'university');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
}
}
function Information(){
Database::connect();
$query = "SELECT * FROM student WHERE id "; $result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
}
Information();
It gives error:
Notice: Undefined variable: db in /Applications/XAMPP/file.php on line 12.
What could be the reason and what am I doing wrong?