我无法解决有关 HEREDOC 语法错误的问题

I am unable to fix an issue concerning an HEREDOC syntax error

这是link为了避免被标记为重复

Issue with heredoc and PHP

按照开发者提供的解决方案,我一直无法解决问题。在这段代码中肯定有一个语法错误,但我找不到它在哪里,事实上下面的 Heredoc 语句不起作用并阻止整个代码工作,此外,如果我尝试 运行 我的网络服务器上的代码我有一个 500 服务器错误。我修改了我的问题,将答案付诸实施。

在编辑这个问题之前,我试图自己解决这个问题,但我已经走进了死胡同。我只是在代码的开头添加了错误报告,即使重新打开旧问题也不是很正确。

   
<?php error_reporting(E_ALL); ini_set('display_errors', 1);?>
<?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
            people_fullname
        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', 'xxxxxxxx') 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;
  
?>

> this is the error that I receive
ENDHTML; /* loop through the results */ while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); 

.= << ENDHTML; }

您的结束标识符 ENDHTML; 之前不应有任何空格,显然每个标识符包含 2 个空格。

  • 删除它们。

错误报告会发现语法错误。

阅读此处文档:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.


脚注:

mysql_* 函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php

此扩展已从 PHP 5.5.0 开始弃用,不建议用于编写新代码,因为它将在未来被删除。相反,要么 mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview 寻求进一步帮助,同时选择 MySQL API。

这些函数允许您访问 MySQL 数据库服务器。有关 MySQL 的更多信息,请访问 » http://www.mysql.com/

可在 » http://dev.mysql.com/doc/.

找到 MySQL 的文档

怎么样

$num_movies = mysql_num_rows($result);
?><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>
<?php   /* 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);
      echo "<tr><td>$movie_name</td><td>$movie_year</td><td>$director</td><td>$leadactor</td><td>$movietype</td></tr>";
    }
    echo "</table><p>$num_movies Movies</p></div>"; 
?>