[英]pre-pared statement query not returning any results
I need to send some database row id's to another page, then use them id's to load the rows and process. 我需要将一些数据库行ID发送到另一页,然后使用它们的ID加载行并进行处理。 I have used a session variable which works fine.
我使用了工作正常的会话变量。
$_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;
}
}
The query would be 该查询将是
SELECT id,jpg WHERE id = 50,51,52
SELECT id,jpg在哪里id = 50,51,52
which should return all of them rows, but instead nothing is being displayed at all, no errors or anything. 它应该返回所有的行,但是却什么也不显示,没有错误或其他任何东西。 Any ideas?
有任何想法吗?
######Edit##### ######编辑#####
Updated my code: 更新了我的代码:
//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;
}
}
Can't seem to do the looping of 似乎无法循环播放
bind_param("s",50);
bind_param(“ s”,50);
as getting the error: 作为得到错误:
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\\xampp\\htdocs\\ppa\\add_sales.php on line 39
警告:mysqli_stmt :: bind_param():变量数与第39行的C:\\ xampp \\ htdocs \\ ppa \\ add_sales.php中已准备好的语句中的参数数不匹配
#####Edit 2##### #####编辑2 #####
Changed the way im going about this, this works fine. 更改了即时通讯的方式,效果很好。
$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);
FROM
clause FROM
子句 $mysqli->error
you would have known it by yourself $mysqli->error
您将自己知道 WHERE id = 50,51,52
is not a valid mysql syntax WHERE id = 50,51,52
不是有效的mysql语法 IN
operator instead IN
运算符 WHERE id = '50,51,52'
. WHERE id = '50,51,52'
。 That's it - an id
column is compared with a single string. id
列与单个字符串进行比较。 SELECT id,jpg WHERE id = 50,51,52
Its wrong statement. 它的错误陈述。
1) You can't write params in where so. 1)你不能在哪里写参数。 Use
where id = 50 or id = 51 or id = 52
where id = 50 or id = 51 or id = 52
2) you missed FROM: FROM Table
2)您错过了FROM:
FROM Table
This is correct: 这是对的:
SELECT id,jpg FROM table WHERE id = 50 or id = 51 or id = 52
您可以尝试以下操作: SELECT id,jpg FROM tablename WHERE id IN (50,51,52);
You need to bind them all separately: 您需要分别绑定它们:
WHERE id IN (?, ?, ?)
And bind: 并绑定:
$stmt->bind_param("i", 50);
$stmt->bind_param("i", 51);
$stmt->bind_param("i", 52);
You will want to use explode()
and a loop to do it properly. 您将需要使用
explode()
和循环来正确执行此操作。 You will also need to use implode to generate the right number of question marks: 您还需要使用implode生成正确数量的问号:
$questionMarks = '(' . implode(',', array_fill(0, count($resultOfSplit), '?')) . ')';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.