【发布时间】:2014-01-21 23:10:42
【问题描述】:
我需要将一些数据库行 ID 发送到另一个页面,然后使用它们的 ID 来加载行和处理。我使用了一个工作正常的会话变量。
$_SESSION['tmp'] = "50,51,52";
if ($stmt = $mysqli->prepare("SELECT id,jpg WHERE id = ?")) {
$stmt->bind_param("s",$_SESSION['tmp']);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result_id,$result_jpg);
if ($stmt->num_rows > 0) {
while($stmt->fetch()) {
$image = array($result_id => $result_jpg);
print_r($image."<br />");
}
} else {
echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
查询将是
选择 id,jpg WHERE id = 50,51,52
它应该返回所有的行,但是根本没有显示任何内容,没有错误或任何东西。有任何想法吗?
######编辑#####
更新了我的代码:
//Generate the query
$query = "SELECT id,jpg FROM images WHERE id IN (?";
for ($i=1; count($_SESSION['tmp']) > $i; $i++) {
$query = $query.",?";
} $query = $query.")";
if ($stmt = $mysqli->prepare($query)) {
for ($i=1; count($_SESSION['tmp']) > $i; $i++) {
$b = $_SESSION['tmp'][$i]-1;
$stmt->bind_param("s",$b);
}
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result_id,$result_jpg);
if ($stmt->num_rows > 0) {
while($stmt->fetch()) {
$image = array($result_id => $result_jpg);
print_r($image);
}
} else {
echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
似乎无法循环
bind_param("s",50);
得到错误:
警告:mysqli_stmt::bind_param(): 变量数不匹配 准备好的语句中的参数数量 C:\xampp\htdocs\ppa\add_sales.php 在第 39 行
#####编辑2#####
改变了我做这件事的方式,这很好用。
$image = array();
foreach($_SESSION['tmp'] as $x) {
if ($stmt = $mysqli->prepare("SELECT id,jpg FROM images WHERE id = ?")) {
$stmt->bind_param("s",$x);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result_id,$result_jpg);
if ($stmt->num_rows > 0) {
while($stmt->fetch()) {
$image[$result_id] = $result_jpg;
}
} else {
echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
}
print_r($image);
【问题讨论】:
-
您尚未指定从哪个表中选择。
-
准备好了,没有准备好。