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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化