0

我只是想学习准备好的语句,我正在按照 PHP 手册指导我完成,我已经在 stackoverflow 上检查了有关此问题的答案,但是,我找不到任何解决方案,$stmt->num_rows 总是(返回 0 )

stackoverflow 上有一篇文章讨论了这个问题,他们建议在 $stmt-num_rows 之前使用 $stmt->store_result(),但 $stmt->num_rows 返回 0

有人可以告诉我我在这里做错了什么......我只是厌倦了程序风格的编码,我想通过准备好的陈述来提高我的技能

这是下面的功能

function get_all()
    {
        // ** Initializing  the Connection
        $mysqli = Connect();
        $sql = ( ' SELECT * FROM `users` ' );
        $stmt = $mysqli->prepare($sql);
        $stmt->execute();
        $res = $stmt->get_result();
        echo $num_count = $stmt->num_rows();
        $user = array();
        for ($counter = 0; $row = $res->fetch_assoc(); $counter++) 
        {
            $user[$counter] = $row;
        }   
        return $user;
    }

// 这是第二次更新

function get_all()
    {
        // ** Initializing  the Connection
        $mysqli = Connect();

        $sql = ( ' SELECT * FROM `users` ' );

        $stmt = $mysqli->prepare($sql);

        $stmt->execute();

        $res = $stmt->get_result();

        echo $num_count = $stmt->num_rows;

        $user = array();

        while($row = $res->fetch_assoc()) 
        {
             $user[] = $row;
        }


         return $user;

    }

// 第三次更新

function get_alll()
    {

        // ** Initializing  the Connection
        $mysqli = Connect();

        // no need to use * character, 
        // need to write query this way
        $sql = ( ' SELECT `id`,`fname`,`lname`,`uname`,`email` FROM `users` ' );

        $stmt = $mysqli->prepare($sql);
        // here need to use bind param
        $stmt->bind_result( $id, $fname, $lname, $uname, $email);
        $stmt->execute();
        // it's important to store the result 
        // before using num rows    
        $res = $stmt->store_result();

        echo $num_count = $stmt->num_rows;

            // 
        while($stmt->fetch())
        {
            echo $fname;
        }

     }
4

1 回答 1

3

num_rows是一个属性,而不是一个方法,尝试$stmt->num_rows不带括号

于 2013-03-21T15:43:18.293 回答