解析错误:语法错误,意外$ end

时间:2015-04-08 08:52:29

标签: php sql syntax-error database-connection parse-error

从数据库创建输出时,我不断收到错误消息。

错误消息:“解析错误:语法错误,第26行的XXXXXXXXX.php意外$结束。”

下面使用的代码。

<?php
    require('connectDB.php');

    if ($connection) {
      echo "Connected to database!";
    } else {
          die("Connection failed: " . mysqli_connect_error());

    $query = "SELECT title, description, body, created FROM diary_post";
    $result = mysql_query($query);

    $retval = mysql_query( $query, $connection );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo "title :{$row['title']}  <br> ".
             "description : {$row['description']} <br> ".
             "body : {$row['body']} <br> ".
             "created : {$row['created']} <br> ".
             "--------------------------------<br>";
    } 
    echo "Fetched data successfully\n";
    mysql_close($conn);
    ?> 

有没有人对为什么这不起作用有任何建议?谢谢。

1 个答案:

答案 0 :(得分:1)

您的else子句缺少结束花括号:

<?php
require('connectDB.php');

if ($connection) {
  echo "Connected to database!";
} else {
      die("Connection failed: " . mysqli_connect_error());
} // This should be here
$query = "SELECT title, description, body, created FROM diary_post";
//$result = mysql_query($query);

$retval = mysql_query( $query, $connection );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "title :{$row['title']}  <br> ".
         "description : {$row['description']} <br> ".
         "body : {$row['body']} <br> ".
         "created : {$row['created']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?> 

建议:使用支持匹配和正确语法突出显示的好IDE,它会使这样的错误非常明显。

编辑:正如评论中建议的@ Rizier123,您不应该收到意外的变量$end错误,请向我们展示connectDB.php的内容,毕竟可能还有更多。