I’ve recently began learning Prepared Statements and have started configuring my scripts with the new style.
However, I ran into a little wall and I’m hoping someone can point me in the right direction?
Here is my SQL query structure:
$query = "SELECT `results`, `Success`, `Failure`, `Counter`, `Grades`, `Classes`,
`Special_Id`, `SpecialCondition` FROM `Courses` INNER JOIN `Students` ON `id` =
`Students_Id` JOIN `Conditions` ON `id` = `Special_Id`";
$stmt = $Mconn->prepare($query);
$stmt->bind_result($results, $success, $failed, $counter, $grades, $classes,
$specialId, $specialcondition);
$stmt->execute();
$stmt->store_result();
And here is one code block:
<?php
echo "<select = \"SpecialConditions\">";
if($stmt->num_rows == NULL){
echo "No results found.";
}else{
while($stmt->fetch()){
echo "<option value=\"$specialId\">$specialcondition</option>";
}
}
echo "</select>";
?>
And another code block:
<?php
echo "<select = \"Grades\">";
if($stmt->num_rows == NULL){
echo "No results found.";
}else{
while($stmt->fetch()){
echo "<option value=\"".$grades."\">".$grades."</option>";
}
}
echo "</select>";
?>
Ok, now what’s happening is if I place both of these queries on the same page, the first block will kill the second one and another query below it and I can’t figure out why and I’ve researched this to no end.
I’ve tried closing the connection within the same code block thinking that might do the trick but to no prevail.
Now, I do know that the DB connection is working and I do know that the returned results are actually being stored because if I test each block on a separate page, it works as it’s supposed to.
Thank you in advance for your help.
Nice, I got it to work finally. Everything needs to be stored in an array and extracted through that it seems. At least that’s the only way I got it to work properly.
Here’s the final solution in case anyone is curious or hits this wall in the future. If you know of a way that does not require this method, please let me know.
My Dynamic function that connects and queries the database:
And here’s the code I use to actually display the data singly or in multiple blocks. Just change the field name to correspond to yours. The function is dynamic and doesn’t require you to hard-code any binding results so you can fetch as many fields as desired.
Thanks for all the help and I hope this serves to help those that ran into the same problem as I did.