2015-07-21 73 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 - 我回答了原来的问题。 –