0

我有当前的代码:

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

有任何想法吗?

4

3 回答 3

1

替换您的以下行:

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

对于这个:

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

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

于 2012-12-12T16:05:54.010 回答
1

我认为这是您的问题,但我建议研究 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>
于 2012-12-12T16:07:56.300 回答
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()返回一个语句句柄,您可以使用它来获取以后的结果。您没有捕获该句柄,因此即使查询已正确执行,您的数据库结果也会被丢弃。

于 2012-12-12T16:06:42.377 回答