Jump to content

[SOLVED] Need help with MySQL syntax error


tommyda

Recommended Posts

When i try to run this script i get an error (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 '' at line 1)

 

 

the purpose of the script is to get all id's (int) from table cat_gambling

 

Then use the id's to grab information from the listings table

 

eventually i will display the info in seperate tables using (while) function

 

<?php

 

include'mysql.php';

 

$result = mysql_query("SELECT * FROM cat_gambling")or die(mysql_error());

$id = $row['id'];

 

while($row = mysql_fetch_array( $result )) {

 

$result2 = mysql_query("SELECT * FROM listings WHERE id = $id")or die(mysql_error());

 

while($row2 = mysql_fetch_array( $result2 )) {

 

echo $row2['site'];

};};

     ?>

 

Can anyone help me find a way to fix it please ?

 

EDIT by toplay:

 

Changed topic subject and moved to MySQL forum.

Link to comment
Share on other sites

Fixed it myself using tizag.com

 

// Get all the data from the "example" table

$result = mysql_query("SELECT * FROM cat_gambling")or die(mysql_error());

 

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

$id = $row['poker'];

$result2 = mysql_query("SELECT * FROM listings WHERE id = $id")or die(mysql_error());

$row2 = mysql_fetch_array($result2) or die(mysql_error());

echo $row2['site'];

};

Link to comment
Share on other sites

The SQL queries themselves seem fine to me provided that the tables exist. Maybe you're getting on a different query and not these. Anyway, you can take the query and run it outside of PHP first and get it working before inserting into the code.

 

You can do this in one query and then you only need to have one "while" loop. Example:

 

SELECT

             l.*

  FROM

             `cat_gambling` cg

  JOIN

             `listings` l

     ON

             l.id = cg.id

 

 

EDIT:

 

Great you fixed it. It's still more efficient to use a join in this case.

 

From your last post it looks like the "ON" of the join should be:  l.id = cg.poker

 

Happy coding.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.