dtmwnqng38644 2015-05-08 15:30
浏览 113
已采纳

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

This is the link in order to avoid to be marked as duplicate

Issue with heredoc and PHP

I haven't been able to solve the issue following the solutions provided by the developers. Within this snippet of code there must be certainly a syntax error but I can't find where it is, as a matter of fact the below Heredoc statement does not work and prevents the whole code from working, furthermore if I try to run the code on my web server I have a 500 server error. I have modified my question implementing the answers into it.

Before editing this question I've tried to solve the problem on my own, but I've gotten to a blind alley. I've just added the error reporting in the beginning of the code, even if it is not very correct to reopen old questions.

   
<?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; }

</div>
  • 写回答

2条回答 默认 最新

  • duanli8391 2015-05-08 15:34
    关注

    There should not be any spaces before your closing identifier ENDHTML; which clearly contains 2 spaces for each of them.

    • Remove them.

    Error reporting would have caught that syntax error.

    Read up on heredoc:

    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 on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.


    Footnotes:

    mysql_* functions deprecation notice:

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

    This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

    These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

    Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。