dpv50040 2015-07-21 21:56
浏览 35

Prepared Statements中num_rows的替代方法是Unbuffered Query

MySQLi Prepared Statements are Unbuffered result set by default which means we can not use the num_rows unless we explicitly create a buffered result set by using $stmt->store_result();.

In my case I just need to use the $stmt->num_rows to make sure that there is a result comming from database. so I used something like

 if($stmt->num_rows > 0){
    while($stmt->fetch())
                {
                 echo  '<li>'.$name.'</li>';
                }
         }else {
                echo "No Such a Data";
               }

now my question is if I would not like to change the unbuffured query to buffered one what is the alternative method to check there is a result in the database server?

  • 写回答

1条回答 默认 最新

  • dongmu1996 2015-07-21 22:02
    关注

    You can simply check if the returned statement is a resource and then look at the row count, then fetch as appropriate.

    $stmt = $mysqliObject->query("SELECT * FROM table");
    
    if(is_resource($stmt) && $stmt->num_rows > 0) {
      // we got a result and have data in this table
      while($item = $stmt->fetch()) {
         // $item is our data from this row
      }
    }
    
    评论

报告相同问题?