我做了一个小项目。 在测试了我的dbconnection后,一切都没问题,我向数据库添加了一些数据并且它工作正常,而不是我想从表中看到一些数据并看到这个通知。 这是我的代码
***index.php***
require_once("../includes/database.php");
require_once("../includes/users.php");
if(isset ($database)) {echo"true";}else {echo "false";}
$sql = "SELECT * FROM users WHERE id = 1";
$result_set = $database->query($sql);
$found_user = $database->fetch_array($result_set);
echo $found_user['username'];
echo "<hr />";
$found_user = User::find_by_id(1);
echo $found_user['username'];
echo "<hr />";
$user_set = User::find_all();
while ($user = $database->fetch_array($user_set)) {
echo "User: ". $user['username'] ."<br />";
echo "Name: ". $user['first_name'] . " " . $user['last_name'] ."<br /><br />";
}
我的文件users.php
class User{
public static function find_all(){
global $database;
$result_set = $database->query("SELECT * FROM `users`");
return $result_set;
}
public static function find_by_id($id=0){
global $database;
$result_set = $database->query("SELECT * FROM `users` WHERE id={$id} LIMIT 1");
$found =$database->fetch_array($result_set);
return $found;
}
}