【问题标题】:I am unable to fix an issue concerning an HEREDOC syntax error我无法解决有关 HEREDOC 语法错误的问题
【发布时间】:2015-07-19 14:08:33
【问题描述】:

这是为了避免被标记为重复的链接

Issue with heredoc and PHP

按照开发人员提供的解决方案,我无法解决问题。在这段代码的 sn-p 中肯定存在语法错误,但我找不到它在哪里,事实上,下面的 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;` 看起来像 ENDHTML; 之前的 2 个空格
  • 为什么要使用heredoc?只需在 html 之前结束 php 并在循环之前启动它
  • 您使用“我的修复”编辑了您的问题,关于您的 heredoc 中的空格,但没有将其标记为编辑。请不要那样做。人们会访问该问题并查看您的新代码和我的答案并说:“那里没有空格,那么为什么要回答?” - 反过来可能会否决我的答案。我回滚到你原来的帖子。

标签: php html heredoc


【解决方案1】:

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

  • 删除它们。

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

阅读heredoc:

警告 需要注意的是,带有结束标识符的行不能包含其他字符,分号 (;) 除外。这尤其意味着标识符可能不会缩进,并且分号之前或之后可能没有任何空格或制表符。同样重要的是要认识到结束标识符之前的第一个字符必须是本地操作系统定义的换行符。这是在 UNIX 系统(包括 Mac OS X)上的 \n。结束分隔符后还必须跟一个换行符。


脚注:

mysql_*函数弃用通知:

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

此扩展自 PHP 5.5.0 起已弃用,不推荐用于编写新代码,因为它将在未来被删除。相反,应该使用mysqliPDO_MySQL 扩展名。在选择 MySQL API 时,另请参阅MySQL API Overview 以获得更多帮助。

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

可以在 »http://dev.mysql.com/doc/ 找到 MySQL 文档。

【讨论】:

  • 我试图从代码中删除空格,我所拥有的是:ENDHTML; /* 遍历结果 */ while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .=
  • @Riccardo 好的,你接受了另一个答案。没关系。我猜 50% 和 50% 等于一个解决方案。
  • 在另一条评论中写到我必须使用 Heredoc,所以我接受了它们,因为两者都是我的解决方案,唯一的问题是我必须使用 Heredoc,所以我必须找到一种方法按照我删除空格的方式继续,它仍然不起作用
  • @Riccardo 只要您有自己的解决方案,您选择哪个正确并不重要。但同样,请不要使用从我的答案或任何其他答案中获取的代码来编辑您的问题。这不是这里的做法。
  • 您能否使用 UPDATE: 更新您的问题:以及在保持第一部分代码不变的情况下不起作用的代码(无法在评论中发布)?
【解决方案2】:

怎么样

$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>"; 
?>

【讨论】:

  • 我已经尝试按照您的回答进行操作,它有效,但我需要使用 Heredoc,因为我正在学习的书使用它们
  • 我们将其称为合资企业 ;-)
  • 啊,好吧,如果书上这么说,那么无论如何:) 但是如果你使用 Fred 的版本,请接受他的回答
  • @mplungjan 没关系。 OP可以选择任何一个。我赞成你的。但是 OP 在他们的问题中进行了编辑,但没有将其标记为编辑,我必须对其进行回滚,以防我被否决。根据我在他们的问题下对他们的评论。 干杯,谢谢。
  • 我需要使用 Heredoc 所以 mplungjan 即使你的答案是正确的,也请阅读 cmets cocerning the other answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多