php mysqli查询期望参数1到字符串 [英] php mysqli query expects parameter 1 be to string

查看:197
本文介绍了php mysqli查询期望参数1到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个User数据库,它调用一个find_all的静态方法,该方法调用一个查询方法,该方法反过来返回数据库的所有值,但是我每次尝试调用它时都会遇到麻烦。这些错误作为mysqli_query()期望参数1为mysqli,字符串在C:\ xampp \htdocs\photogallery\includes\database.php在第20行。我该怎么办?这里是代码片段
User.php

I was coding a User database which calls an static method of find_all which calls an query method which in turn returns all the value of a database but i am finding trouble in doing so each time when i try to call it it gives me these error as mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\photogallery\includes\database.php on line 20.What do i do? Here is the code snippet User.php

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

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

and the index.php where the code is called as follows :

   <?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 on line 20.请帮我。

Each time the error comes as mysqli_query expects parameter 1 to be mysqli,string given in C:\xampp\htdocs\photogallery\includes\database.php on line 20.Help me out please??

推荐答案

当在过程风格中使用 mysqli 时, mysqli_query 的第一个参数必须是连接对象

When using mysqli in a procedural style, the first argument to mysqli_query must be a connection object

从官方文档

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

因此,您需要更改参数的顺序, $ this-> connection 第一个 $ sql 第二个:

So you need to change the order of the arguments, $this->connection goes first and $sql goes second:

$result = mysqli_query($this->connection, $sql);

这篇关于php mysqli查询期望参数1到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆