[php, mysql] Database Query not returning any results

I have a MySQL query I am using to populate some variables on a web page.  I have a similar script working in a larger switch statement on the same file, but there is one case that is not working.

Below is my code:
$query = "SELECT foo.foo1, foo.foo2, foo.foo3, foo.foo4, foo.foo5, foo.foo6, foo.foo7, foo.foo8, foo.foo9, foo.foo10, foo.foo11, foo.foo12, foo.foo13, bar.foo14, bar.foo15, bar.foo16, bar.foo17, bar.foo18, bar.foo19, bar.foo20
     FROM foo, bar
     WHERE '$kit_id' = foo.foo1 AND foo.foo2 = bar.foo12
     ORDER BY foo.foo1 ASC";

$result = mysql_query($query) or die ('Database Access Failed: '.mysql_error());

//setting variables from database with $_ROW 
$row = mysql_fetch_assoc($result);

var_dump ($row);  		
$foo_1	= $row['foo1'];
$foo_2	= $row['foo2'];
$foo_3	= $row['foo3'];
$foo_4	= $row['foo4'];
$foo_5	= $row['foo5'];
$foo_6	= $row['foo6'];
$foo_7	= $row['foo7'];
$foo_8	= $row['foo8'];
$foo_9	= $row['foo9'];
$foo_10	= $row['foo10'];
$foo_11	= $row['foo11'];
$foo_12	= $row['foo12'];
$foo_13	= $row['foo13'];
$foo_14	= $row['foo14'];
$foo_15	= $row['foo15'];
$foo_16	= $row['foo16'];
$foo_17	= $row['foo17'];
$foo_18	= $row['foo18'];
$foo_19	= $row['foo19'];
$foo_20	= $row['foo20'];

Open in new window


The var_dump of the $row variable is showing:
bool(false)

Open in new window


As I said before, this code works on other steps in the process, it is just this one.  The difference is that this grabs the $kit_id variable from a drop down select field from a web form and searches based on the number it pulls from that select statement. That code is added to the get statement and then pulled into the variable $kit_id prior to launching into the query.

A var_dump of the $kit_id variable shows it has a numeric value, so that is not part of the problem. I also get no SQL errors, so it appears to be accessing the database well enough.

Thanks and let me know if there is any other information you want to see.
prileyosborneAsked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
Put $num_rows = mysql_num_rows($result); after the query and echo $num_rows to see how many results are being returned.  If it is 0, then you're not getting any rows returned for your query.

http://us.php.net/manual/en/function.mysql-num-rows.php

For test purposes, I would make two queries, one from each table using only the 'WHERE' for that table to see what you get.
0
 
Ray PaseurCommented:
If var_dump() said it was FALSE, then the value was FALSE.  According to the online PHP manual, FALSE is one of the expected results from that function.  Man page here:
http://php.net/manual/en/function.mysql-fetch-assoc.php
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
Sidebar note:  This error message can be made so much more meaningful if you include the fully resolved query string in the display.  So instead of this...
$result = mysql_query($query) or die ('Database Access Failed: '.mysql_error());

... Try something like this...
$result = mysql_query($query) or die ("FAIL: $query<br/>" . mysql_error());
0
 
prileyosborneAuthor Commented:
Thank you both!  I found the solution by breaking up the statement into two distinct queries instead of trying to combine them, but the note about the result of the query being false helped me trouble shoot the solutions. Thanks again!
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.!
0
 
Ray PaseurCommented:
Bingo!  Glad you're on the right track, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.