2014-11-24 58 views
0

我正在閱讀Wrox的書「Beginning PHP,Apache,MySQL web development」。我一直在逐字追蹤,出於某種原因,我對代碼存在問題。編輯說我的代碼沒有錯誤。但是當我運行它時,會給我以下消息: 「您的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以找到在'第3行'附近使用的正確語法」,這裏是以下代碼問題與heredoc和PHP

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

之後,我試圖複製和粘貼確切的代碼想也許我做錯了什麼 在這裏它下面的代碼:

<?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; //循環結果while(= mysql_fetch_assoc(Resource id#3)){extract(); = get_director(); = get_leadactor(); = get_movietype(); 。= < < ENDHTML; } = < <

任何幫助將不勝感激我的新節目感謝

+2

請,或不使用'mysql_ *'功能(HTTP:/ /stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),他們不再維護,並[正式棄用](https://wiki.php.net/rfc/ mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。您還將希望[防止SQL注入!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – 2014-11-24 15:53:45

+0

您不能有任何空格或製表符之前關閉heredoc的標識符。而且你應該只加入一個查詢來加入必要的表格,不要查詢循環中的其他信息。 – jeroen 2014-11-24 15:54:54

+0

迴應你的'$ query',你會發現它的問題。這些行上的報價問題很可能是'WHERE people_id ='。 $ DIRECTOR_ID;'。 – 2014-11-24 15:54:58

回答

2

當你結束你一定不要把任何字符在該行的開頭的定界符。在你的代碼中,前面有空格或製表符的heredocs。刪除空格並將heredoc放在行的開頭。

0

關閉定界符語句必須是在字符串中的第一個位置:

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