I am trying to make query to a MySQL database to return all columns in a table based on the selected element in a selection list. The query appears to be successful although it is not returning any columns in the query. Any recommendations would be appreciated.
echo "Select a course name<br><br>";
echo "<select name='course' id='course'>";
// Assigns the $result1 to $row1(array) and loops through the array.
while ($row1 = mysqli_fetch_array($result1)) {
if ($row1['code'] == $_POST['course']) {
// Applies the second value of the $row array to the drop down list and assigns the first value of the array as its value.
echo '<option value="' . $row1['code'] . '" selected="selected">' . $row1['course_name'] . '</option>';
} else {
echo '<option value="' . $row1['code'] . '" >' . $row1['course_name'] . '</option>';
}
} // Ends first while loop.
// Finish off the select control.
echo "</select><br><br>";
$course = mysqli_real_escape_string($link2, $_POST['course']);
// Makes a selection query for the course_name and code columns in the courses table.
$result3 = mysqli_query($link2, "SELECT * FROM courses WHERE course_name = '{$course}'");
if($result3 === FALSE){
die(mysqli_error());
}
// Assigns the contents of the array generated from the $result3 query to the name $row3.
$row3 = mysqli_fetch_array($result3, MYSQL_ASSOC);
print_r($row3);
//DEBUG CODE echo "Query 3 successful<br>";
Last edited by unwiredphpnovice on Sat Dec 21, 2013 1:56 pm, edited 1 time in total.
Thanks for responding. I'm not getting any errors.
The $result3 query appears not to have failed but when I try to print the returned array ($row3); I get nothing. It is reading the final echo statement that I used to check with. Do you think there may be a problem with the selection list itself. I used a similar MySQL query earlier on in the code to populate the list and that worked.
Have you tried echoing the query itself, rather than the results? Since it's returning a result object, we know it's syntactically correct. Could it be a case of GIGO?
Thanks for getting back. It appears I pressed the panic button too soon. I didn't check it enough before I posted. I assigned $result3 to mysqli_query($link2, "SELECT * FROM courses WHERE course_name = '{$course}'"); when I should of assigned it to mysqli_query($link2, "SELECT * FROM courses WHERE code = '{$course}'"); as I used $row1['code'] as the option value in the selection list.
My apologies for your time as I should of done a little more checking first. It was in fact a case of GIGO.