2015-07-21 64 views
0

我在第18行發生了錯誤。您可以看到在<<<ENDHTML之後沒有空間,但我無法擺脫錯誤。我甚至認爲我需要把<?php?>覆蓋整個代碼。它也沒有使它工作。我是這樣做的,但我無法擺脫錯誤

<?php 
    $db= mysql_connect('localhost','root','') or die('Unable to connect. Check your connect parameters'); 

    $query = 'CREATE DATABASE IF NOT EXISTS moviesite'; 
    mysql_query($query,$db) or die(mysql_error($db)); 
    mysql_select_db('moviesite', $db) or die(mysql_error($db)); 

    $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)); 

    $num_movies = mysql_num_rows($result); 


//needs to show the same result as table1 
    $table = <<<ENDHTML<div style="text-align: center;"> 
     <h2>Movie Review Database</h2> 
     <table border="0" cellspacing="2" cellpadding="2" style="width:60%; margin: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; 

       while($row = mysql_fetch_array($result)){ 
         extract($row); 
         $table .= <<<ENDHTML<tr> 
         <td>$movie_name</td> 
         <td>$movie_year</td> 
         <td>$movie_director</td> 
         <td>$movie_leadactor</td> 
         <td>$movie_type</td> 
         </tr> 
         ENDHTML; 
       } 
       $table .=<<<ENDHTML</table> 
       <p>$num_movies Movies</p> 
       ENDHTML; 
       </div> 
      ?> 


     <p><?php echo $num_movies?>Movies</p> 
+0

我不確定您是否看過我的原始帖子/回覆,但我已經做了一些修改,您可能沒有看到所做的更改/添加。如果是這樣,請重新加載它以查看更改。 –

回答

1

首先,您需要將標識符後面的代碼放在它下面的新行中。

$table = <<<ENDHTML<div style="text-align: center;"> 

如:

$table = <<<ENDHTML 
<div style="text-align: center;"> 

$table .= <<<ENDHTML<tr> 

$table .= <<<ENDHTML 
<tr> 

和做所有其他的人一樣。

然後,你看到這些?

</tr> 
      ENDHTML; 

  <p>$num_movies Movies</p> 
      ENDHTML; 

它們包含您的交易標識符之前的空間。不應該有任何。

  • 刪除它們

閱讀手冊http://php.net/manual/en/language.types.string.php#language.types.string.syntax.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 \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

錯誤報告會發現這一點,會拋出:

Parse error: syntax error, unexpected end of file...

添加error reporting到您的文件的頂部,這將有助於發現錯誤。

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

// rest of your code 

旁註:錯誤報告只應在分期完成,並且從不生產。

另外,使用mysql_功能下降。他們已被棄用,並將從未來的PHP版本中刪除。使用mysqli with prepared statementsPDO with prepared statements他們更安全。

+0

'mysql_query()期望參數2是資源----然後,mysql_error()期望參數1是資源-----錯誤被顯示 –

+0

@SaugatThapa這是一個完全不同的問題。你需要通過使用錯誤報告和檢查你的數據庫是否沒有失敗通過使用'mysql_error'到你的連接找出失敗的原因http://php.net/manual/en/function.mysql-error.php - 我回答了原來的問題。 –