I would like to get the complete result of a prepared statement as an array (key/value pairs) in order to later use it in a str_replace()
function.
My table has three columns, an index and the fields "x1" and "x2". I used the following successfully:
$db = new mysqli("servername", "username", "pw", "dbname");
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$ps1->bind_result($search, $replace);
$result = array();
while ($ps1->fetch()) {
$result[$search] = $replace;
}
$ps1->close();
}
However, I am thinking that there must be a simpler way, without a while
loop, getting the complete result, not added up from single rows one by one.
I looked at other questions, and I came up with the following, but it doesn't work ("Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result"):
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$result = mysqli_fetch_assoc($ps1);
return $result;
$ps1->close();
}
I also tried $result = mysqli_fetch_all($ps1);
with no success ( getting "Call to undefined function mysqli_fetch_all()").
BTW, I am using PHP 5.6.
from result of prepared select statement as array
No comments:
Post a Comment