tommyda Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/76871-solved-need-help-with-mysql-syntax-error/ Share on other sites More sharing options...
tommyda Posted November 11, 2007 Author Share Posted November 11, 2007 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']; }; Quote Link to comment https://forums.phpfreaks.com/topic/76871-solved-need-help-with-mysql-syntax-error/#findComment-389201 Share on other sites More sharing options...
toplay Posted November 11, 2007 Share Posted November 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/76871-solved-need-help-with-mysql-syntax-error/#findComment-389203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.