Jump to content

Beginner PHP help with mysql error


jamistewart

Recommended Posts

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);

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

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.