Php MySQLi数据库连接

Php MySQLi数据库连接,php,class,mysqli,Php,Class,Mysqli,我(从我第一次尝试数据库类时派生的其他人那里)得到了以下信息: 我这样称呼它: include_once( "system/database.php" ); $query = "SELECT * FROM app"; $dbr = new Database(); //Change this here since your method is query and not $mysqli while( $row = $dbr->Quer

我(从我第一次尝试数据库类时派生的其他人那里)得到了以下信息:

我这样称呼它:

    include_once( "system/database.php" );

    $query = "SELECT * FROM app";     
    $dbr = new Database();  

    //Change this here since your method is query and not $mysqli
    while( $row = $dbr->Query( $query )->fetch_object() ){
       echo '<td>'. $row['id'] . '</td>' ;
       echo '<td>'. $row['title'] . '</td>' ;   
    }  

    Database::Close();
include_once(“system/database.php”);
$query=“从应用程序中选择*”;
$dbr=新数据库();
//在这里进行更改,因为您的方法是query而不是$mysqli
while($row=$dbr->Query($Query)->fetch\u object()){
回显'.$row['id'].';
回显“.$row['title']”;
}  
数据库::Close();
对while循环中的非对象调用成员函数
fetch\u object()
时出错


有什么想法吗?

fetch\u对象使用诸如mysql\u query或使用fetch\u assoc等方法执行查询后返回的结果集

$query->execute();
$result = $query->get_result();
while ($myrow = $result->fetch_assoc()) {
    //Your logic
}

嗯,您的第一次尝试导致代码完全无法使用。
有两个严重故障和一个严重故障

  • 正如我已经告诉过你的,做
    $query=self::$mysqli->real\u escape\u string($query)
    既无用又有害。你必须摆脱这条线。完全地,永远地
  • 准备一个没有绑定变量的查询是完全无用的
  • 您必须检查mysql错误 因此,至少您的query()函数必须是

    public function query($query)
    {
        $res = self::$mysqli->query($query);
        if (!$res)
        {
            throw new Exception(self::$mysqli->error);
        }
        return $res;
    }
    

    但同样-此函数不安全,因为它没有实现占位符来替换查询中的数据

    很可能您的查询失败了,您假设它成功了,现在它就在您面前爆炸了。永远不要链接数据库调用。始终检查每个阶段的错误。很可能是因为您关闭了数据库连接,而mysqli语句句柄中没有
    ->result
    。请查看您的
    Query()
    函数。如果内部的
    if
    语句不为true,则不会返回任何结果。因此,您不能对未定义的变量/对象调用方法
    fetch_object()
    。那么在使用execute之前,$query是什么?$mysqli=newmysqli(“127.0.0.1”、“用户”、“密码”、“世界”)$query=$mysqli->stmt_init()$查询->准备(“从表名中选择*)$查询->执行();
    public function query($query)
    {
        $res = self::$mysqli->query($query);
        if (!$res)
        {
            throw new Exception(self::$mysqli->error);
        }
        return $res;
    }