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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀