dongyinshua9996 2016-04-25 10:55
浏览 60
已采纳

打印预处理语句的结果php(mysqli :: num_rows未正确设置)

I've recently updated from normal statements

$result = mysql_query("SELECT user_id from users WHERE user_id = '$user_id'");

to prepared statements(security)

prepare("SELECT user_id FROM users WHERE user_id = ?");

I followed some tutorials, but this still won't work:

public function isUserRegistered($user_id) {
        print $user_id;
        $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
        $stmt->bind_param("s", $user_id);
        if ($stmt->execute()) {
            $stmt->bind_result($user_id);
            $stmt->fetch();     

            $no_of_rows = $stmt->num_rows;
            print $no_of_rows;
            $stmt -> close();
            if ($no_of_rows > 0) {
                print "Finally";
                // user already registered
                return true;
            } else {
                print "Stupid";
                // user is not registered
                return false;
            }
        }
    }

The supplied id exists, because I can see it in the console being printed. The if ($stmt->execute()) is being executed, but for some reason nothing comes back.

How can I solve this and how can I print the result?

I've also tried:

while ($stmt->fetch()) {
        printf ("%s (%s)
", $user_id);
    }
  • 写回答

2条回答 默认 最新

  • dongtan8122 2016-04-25 15:21
    关注

    The issue is that $stmt->num_rows is not valid unless you store the result first as explained here: mysqli_stmt::$num_rows - Return the number of rows in statements result set

    The fetch works fine and returns the rows of data as expected. Alas, the mun_rows property is zero so the isUserRegistered method fails. Even though it returned the correct data.

    Note: changed to actually check the returned value from the fetch.

    Conclusion:

    • Use $stmt->store_result() to ensure $stmt->num_rows is useful.

    • use the actual returned data to ensure it returned what you expected?

    Example code: http://pastebin.com/wDvAru39

    Final isUserRegistered code I used:

    Code:

    class UserRegister {
    
        protected $conn = null;
    
        public function __construct($dbConnection) 
        {
            $this->conn = $dbConnection;
        }   
    
        public function isUserRegistered($user_id) {
            print $user_id;
            $result_user_id = null;
            $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
            $stmt->bind_param("s", $user_id);
            if ($stmt->execute()) {
    
                $stmt->store_result(); // need this to force the `num_rows` to be correct
    
                $stmt->bind_result($result_user_id);
                $stmt->fetch();     
    
                $no_of_rows = $stmt->num_rows; 
    
                var_dump(__METHOD__, 
                        'input user id: '. $user_id, 
                        'found user id: '. $result_user_id, 
                        'reported number of rows: '. ($no_of_rows),  __FILE__.__LINE__);
    
                $stmt->close();
                if (!empty($result_user_id)) { // check the returned data not the indicator
                    print "Finally";
                    // user already registered
                    return true;
                } else {
                    print "Stupid";
                    // user is not registered
                    return false;
                }
            }
        }    
    }
    

    Output from the var_dump statements:

    Note: the store_result statement.

    12321
    string 'UserRegister::isUserRegistered' (length=30)
    string 'input user id: 12321' (length=20)
    string 'found user id: 12321' (length=20)
    string 'reported number of rows: 1' (length=26)
    string 'K:\developer\testmysql\index4.php78' (length=35)
    Finally
    string 'isUserRegistered : 
    

    Comment out the 'store_result' statement gives:

    12321
    string 'UserRegister::isUserRegistered' (length=30)
    string 'input user id: 12321' (length=20)
    string 'found user id: 12321' (length=20)
    string 'reported number of rows: 0' (length=26)
    string 'K:\developer\testmysql\index4.php79' (length=35)
    Finally
    string 'isUserRegistered : true
    

    Note: number of reported rows is zero.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动