MySQLi编写的语句没有返回任何内容

MySQLi编写的语句没有返回任何内容

问题描述:

I am trying to transition from literal query calls to prepared statements.

I have the following function in my class:

public function read ($identifier) {
    $stmt = static::$mysqli->prepare('SELECT * FROM `table` WHERE `id` = ?;');
    $stmt->bind_param('i', $identifier);
    $stmt->execute();

    var_dump($stmt->num_rows);
}

I know from phpMyAdmin that there is a row with id 1 in the database, but if I call read(1);, num_rows is 0.

However, if I use this logic:

public function read ($identifier) {
    $result = static::$mysqli->query('SELECT * FROM `table` WHERE `id` = '.(int) $identifier.';');
    var_dump($result->num_rows);
}

I get 1 instead.

我正在尝试从文字 query code>调用过渡到预准备语句。 p>

我的课程中有以下功能: p>

 公共函数读取($ identifier){
 $ stmt = static :: $ mysqli-&gt  ; prepare('SELECT * FROM`table` WHERE`id` =?;'); 
 $ stmt-> bind_param('i',$ identifier); 
 $ stmt-> execute(); 
  
 var_dump($ stmt-> num_rows); 
} 
  code>  pre> 
 
 

我从phpMyAdmin知道数据库中有一行id为1,但是如果 我调用 read(1); code>, num_rows code>为0。 p>

但是,如果我使用这个逻辑: p> \ n

 公共函数读取($ identifier){
 $ result = static :: $ mysqli-> query('SELECT * FROM`table` WHERE`id` ='。(int)$ 标识符。';'); 
 var_dump($ result-> num_rows); 
} 
  code>  pre> 
 
 

我得到1。 p> DIV>

Try call $stmt->store_result(); after the execute() call.
The result is not yet stored in memory after execute, you will either have to fetch all rows or call store_result() for the num_rows variable to show the actual number of rows affected.