I need to send some database row id's to another page, then use them id's to load the rows and process. 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
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);
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
#####Edit 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 $mysqli->error
you would have known it by yourself WHERE id = 50,51,52
is not a valid mysql syntax IN
operator instead WHERE id = '50,51,52'
. That's it - an id
column is compared with a single string. SELECT id,jpg WHERE id = 50,51,52
Its wrong statement.
1) You can't write params in where so. Use where id = 50 or id = 51 or id = 52
2) you missed 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. You will also need to use implode to generate the right number of question marks:
$questionMarks = '(' . implode(',', array_fill(0, count($resultOfSplit), '?')) . ')';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.