Solved

[php, mysql] Database Query not returning any results

Posted on 2012-03-27
5
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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