PHP Join problem

DaVisionz

[H]ard|Gawd
Joined
Mar 21, 2001
Messages
1,935
So i decided to start learning PHP and sql. I am working with some buddies at work to make a website for our department.

This query works in the mysql shell

Code:
SELECT * FROM genHotelInfo, sHotelInfo, contactInfo, deviceInfo WHERE genHotelInfo.pCode=\'*****\' AND genHotelInfo.pCode=sHotelInfo.pCode AND sHotelInfo.pCode=contactInfo.pCode AND contactInfo.pCode=deviceInfo.pCode"

but when it's injected into PHP:

PHP:
$result = mysql_query("SELECT * FROM genHotelInfo, sHotelInfo, contactInfo, deviceInfo WHERE genHotelInfo.pCode=\'*****\' AND genHotelInfo.pCode=sHotelInfo.pCode AND sHotelInfo.pCode=contactInfo.pCode AND contactInfo.pCode=deviceInfo.pCode", $mysql_access);

if (!mysql_query($result,$mysql_access))
  {
  die('Error: ' . mysql_error());
  }


I get this:

"Error: Query was empty"


if i make any changes at all to that i get an error message

i get a "resource id#3 at line 1" error message

Please help!!! we were supposed to go live with the site tomorrow, but i can't get my joins to work... (at least it was a deadline we set and not administration)


* the "*****" above is a defined value.
 
Well, you seem to be confusing the mysql_query() function. The second call you make to it is being passed sql that's already been turned into a result. It's like that you mean your second call to mysql_query() to be mysql_fetch_array(). A cleaned up version might look like this:

Code:
<?php
 
$sql = "SELECT * 
     FROM genHotelInfo, sHotelInfo, contactInfo, deviceInfo 
     WHERE genHotelInfo.pCode=\'*****\' 
     AND genHotelInfo.pCode=sHotelInfo.pCode 
     AND sHotelInfo.pCode=contactInfo.pCode 
     AND contactInfo.pCode=deviceInfo.pCode";

if ( !$result = @mysql_query($sql) )
{
     die(mysql_error());
}

if ( !$chunk = @mysql_fetch_array($result, MYSQL_ASSOC) )
{
     die(mysql_error());
}

//
// Play with $chunk now as an array

?>

The code is not tested but should work and at least give you an idea of how to properly use php's mysql functions.
 
Don't escape the single quotes.

It should be

mysql_query("select * from table where x='string' order by rand()")
 
Have you tried using ADOdb?

I used it a while back, paired with postgresql, and it was great. Made things much easier.
 
Back
Top