herdoc和PHP的问题
我正在关注Wrox的《开始PHP、Apache、MySQL web开发》一书。我一直在一字不差地关注它,出于某种原因,我对代码有一个问题。编辑说我的代码中没有错误。但当我运行时,它会给我以下信息: “您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以了解在第3行的“”附近使用的正确语法”以下是代码herdoc和PHP的问题,php,mysql,Php,Mysql,我正在关注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
<?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;//在(=mysql_fetch_assoc(资源id#3)){extract();=get_director();=get_leadactor();=get_movietype()时循环遍历结果;.=当你结束一个heredoc时,你不能在行首放任何字符。在你的代码中,有一些heredoc前面有空格或制表符。删除空格并将你的heredoc放在行首
ENDHTML; // works
ENDHTML; // won’t work
请注意,它们已不再维护,而是。请改为了解,并使用or。您还希望在heredoc的结束标识符之前不能有任何空格或制表符。您只需在必要的表中进行一次查询,不要在循环中查询其他信息。回显您的
$query
,您将看到pro有问题。很可能是这些行的报价问题WHERE people\u id=”。$director\u id;
spaces ENDHTML;
如前所述。删除ENDHTML;
之前和之后的空格。如果设置了,则会给您一个错误。谢谢大家。所以很清楚,没有人会在他们的网站上使用这种方法吗?