Jump to content

Recommended Posts

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>

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.

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?

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'].

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.