MySQL Prepared语句在子查询[重复]中返回意外记录

问题描述 投票:0回答:1

我有下面的代码可以提取所有记录。

$item_ids = [6, 7];
$conn = mysqli_connect("localhost", "root", "", "test");
$comma_seperated_item_ids = implode(",", $item_ids);

$stmt = $conn->prepare("select price, quantity from b_sales_item where id in (?)");
$stmt->bind_param("s", $comma_seperated_item_ids);
$stmt->execute();
$result = $stmt->get_result();

while ($sales = $result->fetch_assoc()) {
    echo "<pre>";
    print_r($sales);
    echo "</pre>";
}           

上面的代码仅输出一条记录,我希望是2。

如果我从]更改下面的代码,一切正常

$stmt = $conn->prepare("select price, quantity from b_sales_item where id in (?)");
$stmt->bind_param("s", $comma_seperated_item_ids);

to

$stmt = $conn->prepare("select price, quantity from b_sales_item where id in (6,7)");
//$stmt->bind_param("s", $comma_seperated_item_ids);

我是否缺少任何内容来输出2条记录?

我有下面的代码可提取所有记录。 $ item_ids = [6,7]; $ conn = mysqli_connect(“ localhost”,“ root”,“”,“ test”); $ comma_seperated_item_ids = implode(“,”,$ item_ids); $ stmt = $ conn-&...

php mysqli prepared-statement
1个回答
0
投票

如果您执行以下操作

© www.soinside.com 2019 - 2024. All rights reserved.