Select where is giving me problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vexus
Forum Newbie
Posts: 8
Joined: Thu Jul 10, 2008 12:33 pm

Select where is giving me problems

Post by vexus »

Hey guys, I am trying to write a script that will pull up certain info from the right row from the table based on 2 variables.

Code: Select all

 
$cheater = 'Samhenry';
$with = 'Nickdoe';
 
$data = mysql_query("SELECT * FROM cheater WHERE Cheater = '$cheater' AND With = '$with'");
$data = mysql_query($data) or die(mysql_error());  
 
Print "<table border cellpadding=3>";
$info = mysql_fetch_array($data);
{
Print "<tr>";
Print "<th>cheater:</th> <td>".$info['Cheater'] . "</td> ";
Print "<th>url:</th> <td>".$info['CheaterURL'] . " </td></tr>";
}
Print "</table>";
?> 
 
I just get "Query was empty" I know 100% the data in the database will match $cheater and $with. :(

Help!
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Select where is giving me problems

Post by WebbieDave »

WITH is a reserved MySQL word.

Try:

Code: Select all

$data = mysql_query("SELECT * FROM cheater WHERE Cheater = '$cheater' AND `With` = '$with'");
Here's a list of reserved words in MySQL 5.0:
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html
vexus
Forum Newbie
Posts: 8
Joined: Thu Jul 10, 2008 12:33 pm

Re: Select where is giving me problems

Post by vexus »

WebbieDave wrote:WITH is a reserved MySQL word.

Try:

Code: Select all

$data = mysql_query("SELECT * FROM cheater WHERE Cheater = '$cheater' AND `With` = '$with'");
Here's a list of reserved words in MySQL 5.0:
http://dev.mysql.com/doc/refman/5.0/en/ ... words.html
Thanks but I just get this:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #2' at line 1

I even changed the db field name to CWith and still get that. :x

New Code:

Code: Select all

 
$cheater = 'Samhenry';
$cwith = 'Nickdoe';
 
$data = mysql_query("SELECT * FROM cheater WHERE Cheater = '$cheater' AND CWith = '$cwith'");
 
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Select where is giving me problems

Post by WebbieDave »

Put the SQL in a variable then echo it to investigate what is being sent to the database.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Select where is giving me problems

Post by jayshields »

I don't think that error is being caused by that call to mysql_query(), because it seems you are passing a resource ID to mysql_query() where the error is triggered, which basically means you're trying to execute a resultset as a query.

Try putting all your queries into a query variable and changing all your mysql_query() calls to something like:

Code: Select all

 
mysql_query($query) or die(mysql_error() . '<br />In ' . __FILE__ . ' on ' . __LINE__ . '<br />Offending query: ' . $query);
 
vexus
Forum Newbie
Posts: 8
Joined: Thu Jul 10, 2008 12:33 pm

Re: Select where is giving me problems

Post by vexus »

I fixed it by using a different method.

Can some one tell me how I could go about retrieving data from db then some how putting different data in different areas of the page without having to echo all the html within the php blocks. Are there global variables or something I don't know about?
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Select where is giving me problems

Post by WebbieDave »

vexus wrote:I fixed it by using a different method.
Wait. How did you fix it? Someone in the future (maybe me) may run into the same problem and read here only to have the last page of the story ripped out!
vexus wrote:Can some one tell me how I could go about retrieving data from db then some how putting different data in different areas of the page without having to echo all the html within the php blocks
What's wrong with PHP blocks? Perhaps you mean without using quoted strings. In that case, PHP was built for such syntax. You can see examples of doing this here:
http://www.php.net/manual/en/language.basic-syntax.php
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Select where is giving me problems

Post by califdon »

I can't help but remember the story of the guy who's driving around San Francisco's financial district, desperately looking for a parking place, since he's already late for a very important meeting. (If you're not familiar with S.F. downtown parking, just take my word for it, it's practically impossible!) He starts praying, "Oh Lord, this is SO important to me! I've just got to park this car and get to the meeting! I know I haven't been a faithful husband, and I drink too much, and I haven't been to church in years, but if You would only help me find a parking place right NOW, I promise I will quit running around on my wife, and I'll stop drinking, and I'll be in church EVERY Sunday, and ..." Suddenly, right in front of him, miraculously, a car pulls out of a space. The guy darts into the space, as he looks heavenward and says, "...oh, never mind, I just found one!"
Post Reply