As I was looking for an answer to another question, I came across this thread:
bind_result into an array PHP mysqli prepared statement
It answers the very same question. I don't know how to delete my own post, which now I see as a duplicate of previously asked question. Moderator, please feel free to delete this post. Thanks.
I'm converting my php scripts to prepared statements, but I'm having trouble sorting out the right syntax for fetching associative array. The previously built non-prepared version works, but I can't figure out how to retrieve mysqli_result. I have tried various mutation of what I thought might work (some of which noted below), but none worked so far. I either get "object given" or "boolean given". What am I doing wrong? How can I retrieve mysqli_result?
Here is the non-prepared version of the php script that works (simplified to demonstrate the difference between working version vs. non-working prepared-statment version):
// THIS WORKS
$dbh = mysqli_connect('localhost', $db_user, $db_password);
mysqli_select_db($dbh, $db_dbname);
$num = 123456;
$num0 = 0;
$num1 = 1;
$i = 0;
$myList = array(array());
$query = "SELECT col1, col2, col3, col4, col5, colB, colC
FROM tableOne LEFT OUTER JOIN tableTwo ON( tableOne.col3=tableTwo.colA )
WHERE col6='" . $num . "' AND col7='" . $num1 . "'
AND col8='" . $num1 . "' AND col9='" . $num0 . "'";
$data = mysqli_query($dbh, $query);
if ($data) { $dataCount = mysqli_num_rows($data); }
if ($dataCount > 0) {
while( $row = mysqli_fetch_assoc($data) ) {
$thisCol1 = $row['col1'];
$thisCol2 = $row['col2'];
$thisCol3 = $row['col3'];
$thisCol4 = $row['col4'];
$thisCol5 = $row['col5'];
if ($thisCol1 > $thisCol2) {
$myList[$i]["result1"] = "good";
} else {
$myList[$i]["result1"] = "bad";
}
if ($thisCol3 > $thisCol4) {
$myList[$i]["result2"] = "good";
} else {
$myList[$i]["result2"] = "bad";
}
$myList[$i]["thisCol5"] = $row['col5'];
$myList[$i]["thisColB"] = $row['colB'];
$myList[$i]["thisColC"] = $row['colC'];
$i = $i + 1;
}
echo json_encode($myList);
} else {
echo "no entry found";
}
Here's the attempt at converting to prepared statement that gives me error:
// THIS GIVES ME ERROR
$mysqli = new mysqli('localhost', $db_user, $db_password, $db_dbname);
$stmt = $mysqli->stmt_init();
$num = 123456;
$num0 = 0;
$num1 = 1;
$i = 0;
$myList = array(array());
$query = "SELECT col1, col2, col3, col4, col5, colB, colC
FROM tableOne LEFT OUTER JOIN tableTwo ON( tableOne.col3=tableTwo.colA )
WHERE col6=? AND col7=? AND col8=? AND col9=?";
if($stmt->prepare($query)){
$stmt->bind_param("iiii", $num, $num1, $num1, $num0);
$stmt->execute() or trigger_error($mysqli->error);
if ($stmt->store_result()) {
$stmt->bind_result($col1,$col2,$col3,$col4,$col5,$colB,$colC);
while mysqli_fetch_assoc($stmt) {
$thisCol1 = $col1;
$thisCol2 = $col2;
$thisCol3 = $col3;
$thisCol4 = $col4;
$thisCol5 = $col5;
if ($thisCol1 > $thisCol2) {
$myList[$i]["result1"] = "good";
} else {
$myList[$i]["result1"] = "bad";
}
if ($thisCol3 > $thisCol4) {
$myList[$i]["result2"] = "good";
} else {
$myList[$i]["result2"] = "bad";
}
$myList[$i]["thisCol5"] = $col5;
$myList[$i]["thisColB"] = $colB;
$myList[$i]["thisColC"] = $colC;
$i = $i + 1;
}
echo json_encode($myList);
}
} else {
echo $mysqli->error;
echo "no entry found";
$mysqli->close(); // close connection
}
I tried $rows=$stmt->execute() and mysqli_fetch_assoc($rows), which didn't work. I also tried $rows=$stmt->bind_result($col1,$col2,$col3,$col4,$col5,$colB,$colC), but it didn't work either. I then tried $rows=$stmt->fetch(), again no good.
I can't find the proper syntax for retrieving mysqli_result from the executed statement. I'd so appreciate any and all help.
RESOLVED: Thanks to PeeHaa, I learned how to do this using prepared statement. Here's the first step towards getting $myList to return the array I'm looking for.
// REPLACE THE if($stmt->prepare($query)){} SEGMENT WITH THE FOLLOWING
// AND THE SOLUTION WILL BE STARING AT YOU:
if($stmt->prepare($query)){
$stmt->bind_param("iiii", $num, $num1, $num1, $num0);
$stmt->execute() or trigger_error($mysqli->error);
$rows = $stmt->get_result();
while ($row=mysqli_fetch_array($rows)){
echo "<p>" . $row['col1'] . "</p>";
}
}