我采用了这个例子中给出的代码:PHP 数据库连接类
但是我使用上面的链接更改了代码,如下所示:
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();
它给出了错误:
Notice: Undefined variable: db in /Applications/XAMPP/file.php on line 12.
可能是什么原因,我做错了什么?
智慧大石
哔哔one