jamistewart Posted April 29, 2012 Share Posted April 29, 2012 I am getting this 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 I have absolutely not a clue what that means or where I went wrong, but ANY help would be appreciated. I've checked my DB username, pass and db name and they are all correct. I am getting the error when I type in $subject_set = mysql_query("SELECT * FROM pages WHERE subject_id = ${subject["id"]}", $connection); if (!$subject_set) { die("database query failed: " . mysql_error()); } this is my code: <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php include("includes/header.php"); ?> <div id="sidebar"> <ul> <?php $subject_set = mysql_query("SELECT * FROM subjects", $connection); if (!$subject_set) { die("database query failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)) { echo "<li>{$subject["menu_name"]}</li>"; } $subject_set = mysql_query("SELECT * FROM pages WHERE subject_id = ${subject["id"]}", $connection); if (!$subject_set) { die("database query failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)) { echo "<li>{$subject["menu_name"]}</li>"; } ?> </ul> </div> <div id="content"> <h1 class="sidebar">Content Area</h1> </div> </body> </html> <?php mysql_close($connection); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/ Share on other sites More sharing options...
xyph Posted April 29, 2012 Share Posted April 29, 2012 {$subject["id"]} Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/#findComment-1341564 Share on other sites More sharing options...
jamistewart Posted April 29, 2012 Author Share Posted April 29, 2012 I fixed that and I am still getting the error Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/#findComment-1341565 Share on other sites More sharing options...
xyph Posted April 29, 2012 Share Posted April 29, 2012 You should echo out $subject_set and see what the value is. Keep in mind, you're performing the second query outside of the loop, so $subject will be false. You should program will all error reporting, you'd get a notice error about an undefined key http://php.about.com/od/troubleshooting/qt/php_error_reporting.htm You shouldn't be doing your query this way though. Most modern SQL languages allow things called JOINS, which allow you to pull related data from two tables. SELECT pages.*, subjects.* FROM pages LEFT JOIN subjects ON subject.id = pages.subject_id Using * for joins can cause lost data though, so I suggest specifying what rows you need to select, and creating aliases for those with column name conflicts. There's lots more on Google. I know this is a lot to absorb, but moving into true relational database interactions is a big step, and the things you learn here will be used in almost every future database-driven project you design. Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/#findComment-1341568 Share on other sites More sharing options...
jamistewart Posted April 29, 2012 Author Share Posted April 29, 2012 I'm following the tutorial from Kevin Skoglund from the Lynda tutorials but it might be kinda dated. I've only been learning for 13 days so far so all of this is brand brand new to me. Do you suggest a better place to learn? Thank you for all your replies Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/#findComment-1341569 Share on other sites More sharing options...
gizmola Posted April 29, 2012 Share Posted April 29, 2012 Tutorials are tutorials. If you're happy with the one you're learning from, then keep going. No one tutorials is going to teach you to be a professional level php developer. Like all things it's a process. Quote Link to comment https://forums.phpfreaks.com/topic/261812-beginner-php-help-with-mysql-error/#findComment-1341575 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.