php - 选择查询中的数组

标签 php mysql mysqli

我有以下内容:

<?php

    $array = join(',', $ids); // this prints 3,4,6,7,8

    $stmt = $cxn->prepare('SELECT * FROM comments WHERE id IN (?)');
    $stmt->bind_param('i', $array);
    $stmt->execute();

?>

但是,当我让它打印结果时,它只显示来自第一个 id (3) 的评论,而不显示其他的评论。怎么了?

最佳答案

$arrayCount = count($ids);
$binders = array_fill(0, $arrayCount, '?');

// Create an array of references to the values we want to bind
$bindValues = array(); 
foreach($ids as $key => $id) 
    $bindValues[$keys] = &$ids[$key]; 

// Build SQL statement with the necessary number of bind placeholders
$stmt = $cxn->prepare(
    'SELECT * FROM comments WHERE id IN (' . implode(',', $binders) . ')'
);
// Bind each value (has to be done by reference)
call_user_func_array(array($stmt, "bind_param"), $bindValues));
$stmt->execute();

关于php - 选择查询中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21211442/

相关文章:

mysql - 选择具有相同商品名称的行并显示最低价格

清除权限的 MySQL 用户仍然可以访问数据库

php - 仅显示数据库表中的选定结果

php - MySQL 相当于 pg_fetch_all()

php - 处理查询/多个查询的多个结果时的最佳实践?

php - 如何在 PHP 中读取 iTunes XML 文件

mysql - 用于保存非常不同的小数位数的列类型

php - MySQL 到 MySQLi 分号问题

php - sql 查询对一个查询中多个定义的列进行计数

php - 使用 mySQL 表中的 JOIN 子句获取确切的作者和文章