I am following The book "Beginning PHP, Apache, MySQL web development" by Wrox. I have been following it verbatim and for some reason I am having a issue with the code. The editor says that there are no errors in my code. but when I run it gives me the following message: "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 3" here is the following code
<?php
//take in the id of a director and return his/her full name
function get_director() {
global $db;
$query = 'SELECT people_fullname
FROM people
WHERE people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
//take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT people_fullname
FROM people
WHERE people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
$extract($row);
return $people_fullname;
}
// take in the id of a movie type
// and return the meaningful textual description
function get_movietype($type_id) {
global $db;
$query = 'SELECT movietype_label
FROM movietype
WHERE movietype_id = ' . $type_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
// conect to MySQL
$db = mysql_connect('localhost', 'root', 'root') or
die ('unable to connect. Check your parameters');
// make sure you are yousing the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db) );
// retrieve information
$query = 'SELECT movie_name, movie_year, movie_director, movie_leadactor, movie_type
FROM movie
ORDER BY movie_name ASC, movie_year DESC';
$result = mysql_query($query, $db) or die ($mysql_error($db) );
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
$table = <<<ENDHTML
<div style ="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2" style="width: 70%; margin-left: auto; margin-right:auto;">
<tr>
<th>Movie Title</th>
<th>Year of the release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
ENDHTML;
//loop throught the results
while ($row = mysql_fetch_assoc($result) ) {
extract($row);
$director = get_director($movie_director);
$leadactor = get_leadactor($movie_leadactor);
$movietype = get_movietype($movie_type);
$table .= <<<ENDHTML
<tr>
<td>$movie_name</td>
<td>$movie_year</td>
<td>$director</td>
<td>$leadactor</td>
<td>$movietype</td>
</tr>
ENDHTML;
}
$table .= <<<ENDHTML
</table>
<p>$num_movies Movies</p>
</div>
ENDHTML;
echo $table
?>
After that I tried copying and pasting the exact code thinking maybe I did something wrong and here it is the following code:
<?php
// take in the id of a director and return his/her full name
function get_director($director_id) {
global $db;
$query = 'SELECT
people_fullname
FROM people
WHERE
people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT
FROM
people WHERE
people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
// take in the id of a movie type and return the meaningful textual
// description
function get_movietype($type_id) {
global $db;
$query = 'SELECT
movietype_label
FROM
movietype
WHERE
movietype_id = ' . $type_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
//connect to MySQL
$db = mysql_connect('localhost', 'root', 'root') or
die ('Unable to connect. Check your connection parameters.');
// make sure you’re using the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// retrieve information
$query = 'SELECT
movie_name, movie_year, movie_director, movie_leadactor,
movie_type
FROM
movie
ORDER BY
movie_name ASC,
movie_year DESC';
$result = mysql_query($query, $db) or die(mysql_error($db));
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
$table = <<<ENDHTML
<div style="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2"
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
ENDHTML;
// loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$director = get_director($movie_director);
$leadactor = get_leadactor($movie_leadactor);
$movietype = get_movietype($movie_type);
$table .= <<<ENDHTML
<tr>
<td>$movie_name</td>
<td>$movie_year</td>
<td>$director</td>
<td>$leadactor</td>
<td>$movietype</td>
</tr>
ENDHTML;
}
$table .= <<<ENDHTML
</table>
<p>$num_movies Movies</p>
</div>
ENDHTML;
echo $table;
?>
when I ran the code this time, the table header shows but part of the code is also displayed on the browser. It looks like this: ENDHTML; // loop through the results while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .= << ENDHTML; } .= <<
any help will be appreciated I am new to programming thanks
Closing heredoc statement must be at the very first position in the string:
When you end an heredoc you must not put any char at the begining of the line. In your code there are heredocs with spaces or tabs before them. Delete the spaces and put your heredoc at the begining of the line.