Making MySQL SELECT query base on selection list

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Making MySQL SELECT query base on selection list

Post by unwiredphpnovice »

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.

Here is what the selection list looks like:

Code: Select all


 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>";
Here is the code to generate a new query:

Code: Select all


$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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Making MySQL SELECT query base on selection list

Post by Celauran »

What errors are you encountering?
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Making MySQL SELECT query base on selection list

Post by unwiredphpnovice »

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.

Code: Select all


var_dump($result3);  // prints: object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) } 
var_dump($row3);  // prints: NULL

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Making MySQL SELECT query base on selection list

Post by Celauran »

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?
unwiredphpnovice
Forum Newbie
Posts: 15
Joined: Mon Nov 25, 2013 8:02 am

Re: Making MySQL SELECT query base on selection list

Post by unwiredphpnovice »

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.
Post Reply