Heredoc和PHP的问题 [英] Issue with heredoc and PHP

查看:67
本文介绍了Heredoc和PHP的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Wrox撰写的《入门PHP,Apache,MySQL Web开发》一书.我一直在逐字关注它,由于某种原因,我在代码方面遇到了问题.编辑说我的代码没有错误.但是当我运行它时,会显示以下消息: 您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以找到在第3行的''附近使用正确的语法",这是以下代码

I am following The book "Beginning PHP, Apache, MySQL web development" by Wrox. 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

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

这一次我运行代码时,表头显示了,但是部分代码也显示在浏览器上.看起来像这样: ENDHTML; //遍历结果,而(= mysql_fetch_assoc(资源ID#3)){extract(); = get_director(); = get_leadactor(); = get_movietype(); .=<< ENDHTML; }.=<<

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; // loop through the results while ( = mysql_fetch_assoc(Resource id #3)) { extract(); = get_director(); = get_leadactor(); = get_movietype(); .= << ENDHTML; } .= <<

任何帮助将不胜感激,我是编程新手.

any help will be appreciated I am new to programming thanks

推荐答案

结束Heredoc时,请勿在行首添加任何字符.在您的代码中,heredocs之前带有空格或制表符.删除空格,并将您的Heredoc放在该行的开头.

When you end an heredoc you must not put any char at the begining of the line. In your code there are heredocs with spaces or tabs before them. Delete the spaces and put your heredoc at the begining of the line.

这篇关于Heredoc和PHP的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆