$ mysqli-> fetch_object无法获得迭代结果 - PHP - Mysql

时间:2012-12-12 15:59:18

标签: php mysqli fetch

我有当前的代码:

    Database::connect();
?>

<table>
    <thead>
        <tr>
            <th>ID</th> 
            <th>Title</th>
        </tr>
    </thead>
    <tbody>
        <?php       
            $query = "SELECT * FROM app";  
            $dbr = Database::query( $query ); 
            while( $row = Database::$mysqli->fetch_object( $dbr->result ) ){
                ?>
                <tr>
                    <td><?php echo $row->id; ?></td> 
                    <td><?php echo $row->title; ?></td>
                </tr>
                <?php
            }
        ?>
    </tbody>
</table>        

<?php
    Database::close();
?>

这里是Database和DatabaseQuery类

class DatabaseQuery{
    public $result;
    public $mysql_num_rows;        
}

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

    public static function connect(){  
        self::$mysqli = new mysqli( self::$host, self::$username, self::$password, self::$db_name );

        if (self::$mysqli->connect_error) {
            die('Connect Error (' . self::$mysqli->connect_errno . ') '
                . self::$mysqli->connect_error);
        }
    }

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

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

            $DatabaseQuery = new DatabaseQuery();
                $DatabaseQuery->result = $stmt;       
                $DatabaseQuery->mysql_num_rows = $stmt->num_rows();                 
                $stmt->close();
                return $DatabaseQuery;                   
        }
    }

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

我的调用代码出错了:致命错误:调用未定义的方法mysqli :: fetch_object()

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

替换以下行:

while( $row = Database::$mysqli->fetch_object( $dbr->result ) ){

这个:

while( $row = $dbr->fetch_object( $dbr->result ) ){

因为fetch_object()是mysqli_result对象的方法,而不是一般的mysqli对象。

答案 1 :(得分:0)

你的逻辑被破坏了:

    $query = self::$mysqli->real_escape_string( $query );

您无法转义整个查询。您将要插入查询的数据转义为例如如果你有一些像

那样的东西
 SELECT id, name, ... WHERE firstname = '$searchterm'

你将逃脱$searchterm中的值。转义整个查询会将其转换为

 SELECT id, name, ... WHERE firstname = \'$searchterm\'

并且你最终会出现语法错误,因为你不再在$ searchterm周围有引号,你有一些被忽略的字符作为裸字符串的一部分。

然后是:

        $stmt->store_result();            

store_result()返回一个语句句柄,可以用来从以后获取结果。您没有捕获该句柄,因此即使查询已正确执行,您的数据库结果也会被丢弃。

答案 2 :(得分:0)

我认为这是你的问题,但我建议调查PDO是一种访问数据库并使用它们的一种非常简单的方法。

<?php       
        $query = "SELECT * FROM app";  
        $dbr = Database::query( $query ); 

            //Change this here since your method is query and not $mysqli
            while( $row = Database::$dbr->fetch_object( $dbr->result ) ){
            ?>
            <tr>
                <td><?php echo $row->id; ?></td> 
                <td><?php echo $row->title; ?></td>
            </tr>
            <?php
        }
    ?>

如果你不介意我会说你的课程很复杂。如果你有理由,那么如果不是你就可以这样做。

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

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

         if (self::$mysqli->connect_error) {
        die('Connect Error (' . self::$mysqli->connect_errno . ') '
            . self::$mysqli->connect_error);
        }
    }

    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;                   
    }
}
}

使用类

的文件
 //Include the file unless u have a autoloader
    <tr>
    <?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['IDcolumnName'] . '</td>' ;
           echo '<td>'. $row['TitlecolumnName'] . '</td>' ;


    }
?>
</tr>