douzhi1919 2017-05-23 19:35
浏览 55
已采纳

当等效的普通查询返回3时,Mysqli预处理语句返回0行

I am having some problems with prepared statements in Mysqli and I’m not sure why.

I have a database which currently has 3 rows, which I want to select using a SELECT WHERE query. The query which works in PhpMyAdmin is:

SELECT `totalhits`, `totalmisses`, `date` FROM `performance` WHERE `domain` = 'test' AND `profileid` = 1 ORDER BY `date` DESC

This shows all three rows (all have domain = test and profileid=1.)

If I run this with a normal query in Mysqli and hard-coded variables, I get the same result:

$query = $conn->query(“SELECT `totalhits`, `totalmisses`, `date` FROM `performance` WHERE `domain` = 'test' AND `profileid` = 1 ORDER BY `date` DESC”);
echo $query->num_rows; //outputs 3

If I try and run it as a parameter query (as I will be using user entered data), I get 0 rows returned:

$stmt = $conn->prepare("SELECT `totalhits`, `totalmisses`, `date` FROM `performance` WHERE `domain` = ? AND `profileid` = ? ORDER BY `date` DESC");
$domain = 'test';
$profileid = 1;
$stmt->bind_param('si', $domain,$profileid);
$stmt->execute();
echo $stmt->num_rows; //outputs 0

No Mysqli errors are generated by any of these lines (using print_r on the object at each point to check). I also added a $stmt->store_result() line after the execute line but still had the same result (should I be doing this anyway?).

I know I must be going wrong somewhere but I haven’t read anything in the manual or documentation that might give me a hint as to where. All help would really be appreciated!

If helpful, the SQL database is setup as:

CREATE TABLE `performance` (
  `performanceid` int(11) NOT NULL,
  `domain` varchar(128) NOT NULL,
  `profileid` int(8) NOT NULL,
  `totalhits` int(11) NOT NULL,
  `totalmisses` int(11) NOT NULL,
  `date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `performance` (`performanceid`, `domain`, `profileid`, `totalhits`, `totalmisses`, `date`) VALUES
(1, 'test', 1, 0, 1, '2017-05-15'),
(2, 'test', 1, 1, 1, '2017-05-23'),
(3, 'test', 1, 0, 0, '2017-05-23');
  • 写回答

1条回答 默认 最新

  • douyong6589 2017-05-23 19:46
    关注

    The documentation for mysqli_stmt::num_rows misses some detailed information about using num_rows with prepared statements. The description is rather ambiguous in that it refers only to the need to store the result when using the procedural style, but the object-oriented example makes it clear that you need to call the store_result() method before accessing the num_rows property. This means your code should be something like this:

    $stmt = $conn->prepare("SELECT `totalhits`, `totalmisses`, `date` FROM `performance` WHERE `domain` = ? AND `profileid` = ? ORDER BY `date` DESC");
    $domain = 'test';
    $profileid = 1;
    $stmt->bind_param('si', $domain,$profileid);
    $stmt->execute();
    $stmt->store_result();
    echo $stmt->num_rows; //should now output 3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值