Solved

[php, mysql] Database Query not returning any results

Posted on 2012-03-27
5
283 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:prileyosborne
  • 2
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 400 total points
ID: 37773346
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 37774003
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
 

Author Closing Comment

by:prileyosborne
ID: 37774404
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37774500
You're welcome.!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37774749
Bingo!  Glad you're on the right track, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question