Solved

[php, mysql] Database Query not returning any results

Posted on 2012-03-27
5
281 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 82

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 108

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 82

Expert Comment

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

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now