Solved

[php, mysql] Database Query not returning any results

Posted on 2012-03-27
5
282 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 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

863 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

26 Experts available now in Live!

Get 1:1 Help Now