我編寫了一個用戶數據庫,它調用find_all的靜態方法,該方法調用一個查詢方法,該方法反過來返回數據庫的所有值,但我每次都發現這樣做的麻煩當我嘗試調用它時,它給了我這些錯誤,因爲mysqli_query()期望參數1是mysqli,字符串在C:\ xampp \ htdocs \ photogallery \ includes \ database.php中給出。我該怎麼辦?這裏是代碼片段 user.php的php mysqli查詢期望參數1是字符串
require_once("database.php");
class User{
public static function find_all(){
global $database;
$sql= "select * from users";
$result_set = $database->query($sql);
return $result_set;
}
public static function find_by_id($id=0){
global $database;
$result_set = $database->query("SELECT FROM users where id={$id}");
$found = $database->fetch_array($result_set);
return $found;
}
}
database.php中
public function query($sql){
echo $sql;
$this->last_query = $sql;
echo $result = mysqli_query($sql,$this->connection);
$this->confirm_query($result);
return $result;
}
public function confirm_query($result){
if(!$result){
die("Database query failed:".mysqli_error());
}
}
並且其中所述代碼被稱爲如下在index.php:
<?php
require_once("../includes/database.php");
require_once("../includes/user.php");
$q = User::find_all();
?>
每次誤差因爲mysqli_query期望參數1是mysqli,字符串在C:\ xampp \ htdocs \ photogallery \ includes \ database.php中有第20行。請幫我?
使用'mysqli_query($這個 - >連接,$ SQL);'第一參數是「連接變量」,第二個是「查詢變量」。 – RJParikh