I am getting values from an associativearray
$datos = "";
$array = array(1, 2, 3, 4);
foreach ($array as $valor => $dato) {
$datos = $datos . $dato . ",";
}
$app = rtrim($datos, ",");
echo $app;
Result: 1,2,3,4
Now it's strange I don't understand what is happening in an object-oriented style procedure in MySQLi it only shows me a result and ignores the rest.
$stmt = $con->prepare("SELECT id, notice, tag FROM news WHERE id in (?)");
$stmt->bind_param('i',$app);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $notice, $tag);
while ($stmt->fetch()) {
echo '<div class="content-notice">
<h1>'.$notice.'</h1>
<span>'.$tag.'</span>
</div>';
}
I have tried changing the integer (i)
to string
(s)
but it gives the same result, it only shows one result...
On the other hand, in a different procedure without object-oriented there if it prints the 4 results.
$sql = "SELECT * FROM news WHERE id in ({$app})";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($query)){
echo '<h1>'.$row['notice'].'</h1>';
}
Can you explain to me what is happening?
Oscar tests this code.
$arrIN
is an array with the ids you want to use as criteria in theIN...
?
that the prepared query will carry will be created.$strTipos
with the data types for each one, in this case I assume they are of the integer type, that's why thei
.The code would look more or less like this:
My final recommendation is to move to PDO. All this fuss is due to one thing, mysqli doesn't support passing parameters directly in the
execute
! With PDO this would have been solved with a line or two of code. However, look at all the twists and turns withmysqli
.You can read about this interesting article published in phpdelusions.net: Why mysqli prepared statements are so hard to use? - ( Why are prepared queries so hard to use in mysqli? )