简体   繁体   English

Heredoc和PHP的问题

[英]Issue with heredoc and PHP

I am following The book "Beginning PHP, Apache, MySQL web development" by Wrox. 我正在关注Wrox撰写的《 PHP,Apache,MySQL Web开发入门》一书。 I have been following it verbatim and for some reason I am having a issue with the code. 我一直在逐字关注它,由于某种原因,我在代码方面遇到了问题。 The editor says that there are no errors in my code. 编辑说我的代码没有错误。 but when I run it gives me the following message: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3" here is the following code 但是,当我运行它时,会显示以下消息:“您的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
  ?>

After that I tried copying and pasting the exact code thinking maybe I did something wrong and here it is the following code: 之后,我尝试复制并粘贴确切的代码思维,可能是我做错了,这里是以下代码:

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

when I ran the code this time, the table header shows but part of the code is also displayed on the browser. 这次我运行代码时,表头显示了,但是部分代码也显示在浏览器上。 It looks like this: ENDHTML; 看起来像这样:ENDHTML; // loop through the results while ( = mysql_fetch_assoc(Resource id #3)) { extract(); //遍历结果,同时(= mysql_fetch_assoc(资源ID#3)){extract(); = get_director(); = get_director(); = get_leadactor(); = get_leadactor(); = get_movietype(); = get_movietype(); .= << ENDHTML; 。= << ENDHTML; } .= << }。= <<

any help will be appreciated I am new to programming thanks 任何帮助将不胜感激,我是编程新手,谢谢

When you end an heredoc you must not put any char at the begining of the line. 当您结束Heredoc时,请勿在行首放置任何字符。 In your code there are heredocs with spaces or tabs before them. 在您的代码中,heredocs之前带有空格或制表符。 Delete the spaces and put your heredoc at the begining of the line. 删除空格并将您的Heredoc放在行首。

Closing heredoc statement must be at the very first position in the string: 关闭heredoc语句必须在字符串的第一个位置:

ENDHTML;    // works
  ENDHTML;  // won’t work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM