我正在关注Wrox的“开始PHP,Apache,MySQL Web开发”一书。我一直在逐字逐句,因为某些原因我遇到了代码问题。编辑说我的代码中没有错误。但是当我运行它时,给我以下消息: “您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第3行附近使用正确的语法”,这里是以下代码
<?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
?>
之后我尝试复制并粘贴确切的代码,可能是我做错了 这里是以下代码:
<?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;
?>
这次我运行代码时,表头显示但部分代码也显示在浏览器上。它看起来像这样: ENDHTML; //循环结果while(= mysql_fetch_assoc(Resource id#3)){extract(); = get_director(); = get_leadactor(); = get_movietype(); 。=&lt;&lt; ENDHTML; }。=&lt;&lt;
任何帮助将不胜感激我是编程感谢的新手
答案 0 :(得分:2)
当你结束heredoc时,你不能在行的开头放任何字符。在你的代码中,有一些带有空格或标签的heredocs。删除空格并将你的heredoc放在行的开头。
答案 1 :(得分:0)
关闭heredoc语句必须位于字符串中的第一个位置:
ENDHTML; // works
ENDHTML; // won’t work