amin7b5 Posted April 16, 2008 Share Posted April 16, 2008 Hello. I am trying to learn php/mysql and am currently working out of a wrox book "beginning php apache and mysql". I'm trying to execute a simple query to the database to display a list of movies with the lead actor and directors name. I followed the code in the book, however when I execute the script the following error is displayed: 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 been banging my head against this for the past several days and cannot figure out where the syntax is wrong. If I eliminate the call to the two functions, the error goes away. Here is the PHP code... any help in understanding this would be very much appreciated. Thank you in advance! <?php //connect to db mysql_connect("localhost", "user", "pass") or die("Check your server connection!"); mysql_select_db("moviesite"); //function to get lead actor function get_leadactor($lead_actor) { $query_leadactor = "SELECT people_fullname FROM people WHERE people.people_id = $lead_actor"; $results = mysql_query($query_leadactor) or die(mysql_error()); $rows = mysql_fetch_array($results); extract ($rows); $actorname=$people_fullname; global $actorname; } //function to get lead director function get_leaddirector($lead_director) { global $directorname; $query_leaddirector = "SELECT people_fullname FROM people WHERE people.people_id = $lead_director"; $results = mysql_query($query_leaddirector) or die(mysql_error()); $rows=mysql_fetch_array($results); extract($rows); $directorname = $people_fullname; } echo "<table border='1'>\n"; echo "<tr>\n"; echo "<td><strong>Movie Name</strong></td>\n"; echo "<td><strong>Actor Name</strong></td>\n"; echo "<td><strong>Director Name</strong></td>\n"; echo "</tr>\n"; $query = "SELECT * FROM movie"; $results = mysql_query($query) or die(mysql_error()); while ($rows = mysql_fetch_assoc($results)) { get_leadactor($movie_leadactor); get_leaddirector($movie_director); //build table echo "<tr>\n"; echo "<td>"; echo $movie_name; echo "</td>\n"; echo "<td>"; echo $actorname; echo "</td>\n"; echo "<td>"; echo $directorname; echo "</td>\n"; echo "</tr>\n"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/ Share on other sites More sharing options...
benphp Posted April 16, 2008 Share Posted April 16, 2008 I believe you need single quotes: WHERE people.people_id = $lead_director" WHERE people.people_id = '$lead_director' " Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/#findComment-518787 Share on other sites More sharing options...
Daniel0 Posted April 16, 2008 Share Posted April 16, 2008 It seems that you're aren't passing any data to the get_leadactor() and get_leaddirector() functions. The $movie_leadactor and $movie_director variables are never set within the while loop. I believe you need single quotes: WHERE people.people_id = $lead_director" WHERE people.people_id = '$lead_director' " Only on non-integer values, which an ID probably isn't. Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/#findComment-518788 Share on other sites More sharing options...
amin7b5 Posted April 16, 2008 Author Share Posted April 16, 2008 I believe you need single quotes: WHERE people.people_id = $lead_director" WHERE people.people_id = '$lead_director' " I added the single quotes and that seems to solve the MySQL error, however now the error displays: Warning: extract() [function.extract]: First argument should be an array in C:\xampp\htdocs\Sites\php\movieactordirector.php on line 11 Warning: extract() [function.extract]: First argument should be an array in C:\xampp\htdocs\Sites\php\movieactordirector.php on line 21 It seems that you're aren't passing any data to the get_leadactor() and get_leaddirector() functions. The $movie_leadactor and $movie_director variables are never set within the while loop Wouldn't the $movie_leadactor and $movie_director variable be set when the query: SELECT * FROM movie is executed? That does get called in the while loop: while ($rows = mysql_fetch_assoc($results)) Thanks so much for the replies. Am I totally misunderstanding this? Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/#findComment-518813 Share on other sites More sharing options...
Daniel0 Posted April 16, 2008 Share Posted April 16, 2008 It seems that you're aren't passing any data to the get_leadactor() and get_leaddirector() functions. The $movie_leadactor and $movie_director variables are never set within the while loop Wouldn't the $movie_leadactor and $movie_director variable be set when the query: SELECT * FROM movie is executed? That does get called in the while loop: while ($rows = mysql_fetch_assoc($results)) Thanks so much for the replies. Am I totally misunderstanding this? No. They would be indexes of the $rows array. I.e. $rows['movie_leadactor'] and $rows['movie_director']. Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/#findComment-518826 Share on other sites More sharing options...
amin7b5 Posted April 17, 2008 Author Share Posted April 17, 2008 SOLVED! After adding: extract($rows); to the query before the functions are called, everything works great. Thanks so much for the replies! Quote Link to comment https://forums.phpfreaks.com/topic/101424-solved-cant-figure-out-simple-syntax-error/#findComment-519641 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.