【问题标题】:How to bind params with mysqli query如何使用 mysqli 查询绑定参数
【发布时间】:2016-02-14 03:30:01
【问题描述】:

我现在正在为应该相当简单的事情苦苦挣扎一个多小时。 我想从我的 mysqli 数据库中获取行,当我从 phpmyadmin 运行查询时,我得到了预期的结果,但是当我从我的 php 代码运行它(with bind_param)时,我得到 0 个结果:

$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";
}

【问题讨论】:

  • 您是否遇到任何错误?
  • 无,我试图得到任何错误消息,但没有任何
  • @Lenny 我添加了一个应该可以解决您的问题的答案。

标签: php mysqli bindparam


【解决方案1】:

您需要为 MySQLi 准备好的语句存储结果集。调整你的代码如下:

// Execute query
$sql->execute();

// Store result
$sql->store_result();

echo $sql->num_rows;

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

【讨论】:

  • store_result() 是。我已经在拉我的头发了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2010-12-27
  • 1970-01-01
相关资源
最近更新 更多