【问题标题】:MySQLi database connectionMySQLi 数据库连接
【发布时间】:2013-02-11 14:37:33
【问题描述】:

我有这个(来自我第一次尝试数据库类的其他人):

    require_once( "declarations.php" ); 

    class Database{            
        private static $mysqli;   
        private static $dbName = '';
        private static $username = '';
        private static $password = '';   
        private static $host = 'localhost';
        private static $prefix = '';   

        public function __construct(){  
            if( self::$host & self::$username & self::$password & self::$dbName )
            {
                self::$mysqli = new mysqli( self::$host, self::$username, self::$password, self::$dbName );

                 if (self::$mysqli->connect_error) {
                die('Connect Error (' . self::$mysqli->connect_errno . ') '
                    . self::$mysqli->connect_error);
                }
            }
            else
            {
                echo "You forgot to fill in your database connection details";
            }
        }

        public function Query( $query ){
            $query = self::$mysqli->real_escape_string( $query );

            if ($query = self::$mysqli->prepare($query)) {
                $query->execute();
                $query->store_result();            


                $stmt = $query->result;        
                //$query->mysql_num_rows = $stmt->num_rows();                 
                $query->close();
                return $stmt;                   
            }
        }  

        public function Close()
        {
            self::$mysqli->close(); 
        }
    }                     

我是这样称呼它的:

    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();

我在 while 循环中的非对象上调用成员函数 fetch_object() 时收到错误消息。

有什么想法吗?

【问题讨论】:

  • 很可能你的查询失败了,你认为它成功了,现在它在你面前被炸毁了。永远不要链接数据库调用。 始终在每个阶段检查错误。很可能是因为你关闭了数据库连接,而mysqli语句句柄中没有-&gt;result
  • 看看你的Query()函数。如果里面的if 语句不正确,它不会返回任何结果。所以你不能在未定义的变量/对象上调用方法fetch_object()

标签: php class mysqli


【解决方案1】:

fetch_object 适用于使用以下方法执行查询后返回的结果集:mysql_query 或使用 fetch_assoc 代替

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

【讨论】:

  • 那么在使用execute之前的$query是什么?
  • $mysqli = new mysqli("127.0.0.1", "user", "password", "world"); $query = $mysqli->stmt_init(); $query->prepare("SELECT * FROM tablename"); $query->execute();
【解决方案2】:

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

  1. 正如我已经告诉过你的那样,做$query = self::$mysqli-&gt;real_escape_string( $query ); 既无用又有害。你必须摆脱这条线。完全和永远。
  2. 准备一个没有绑定变量的查询是完全没用的。
  3. 您必须检查 mysql 错误。

所以,至少你的 query() 函数必须是

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

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

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 2021-05-28
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多