doufulian4076 2015-11-12 17:33
浏览 29
已采纳

如何使用mysqli查询绑定params

I am struggling for over an hour now with something that should be fairly simple. I want to get rows from my mysqli database and when I run my query from phpmyadmin I get expected results but when I run it (with bind_param) from my php code I get 0 results:

$sql = $connection->prepare('SELECT (UNIX_TIMESTAMP(start_time)) 
                             FROM table1 
                             WHERE UNIX_TIMESTAMP(start_time) >= ? 
                               AND (UNIX_TIMESTAMP(start_time)) <= ? 
                               AND column3 = ?') 
                            or trigger_error($connection->error, E_USER_ERROR);
$sql->bind_param('sss', $value1, $value2, $value3);
$sql->execute() or trigger_error($sql->error, E_USER_ERROR);

if ($sql->num_rows == 0){
    echo "yay";
}
  • 写回答

1条回答 默认 最新

  • donkey199024 2015-11-12 17:44
    关注

    You need to store the result set for MySQLi prepared statements. Adjust your code as follows:

    // Execute query
    $sql->execute();
    
    // Store result
    $sql->store_result();
    
    echo $sql->num_rows;
    

    PHP docs: http://php.net/manual/en/mysqli-stmt.store-result.php http://php.net/manual/en/mysqli-stmt.num-rows.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效