-2

I keep getting an error message when creating an output from my database.

ERROR MESSAGE: "Parse error: syntax error, unexpected $end in XXXXXXXXX.php on line 26."

Code being used it below.

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

Does anyone have any suggestions to why this isn't working? Thank you.

  • `XXXXXXXXX.php` And which file is xxxxx ? Because you don't have `$end` in this code! So the error is not in the code which you show us! – Rizier123 Apr 08 '15 at 08:53
  • You're missing a `}` after your mysqli connection error statement. – jeroen Apr 08 '15 at 08:54

1 Answers1

1

Your else clause is missing the closing curly bracket:

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

Suggestion: use a good IDE with bracket-matching and proper syntax highlighting, it will make errors like this quite obvious.

Edit: As @Rizier123 suggested in the comments, you shouldn't be getting an unexpected variable $end error, please show us the contents of connectDB.php as well, there may be more to this after all.

kalatabe
  • 2,909
  • 2
  • 15
  • 24
  • Still doesn't explain why OP gets: `unexpected $end`. I just guess that the error is in `connectDB.php` which OP doesn't show us here and he never reaches this line here – Rizier123 Apr 08 '15 at 08:56
  • It does - PHP finished analyzing the file and it `end`-ed when it shouldn't have, because a statement was not terminated with a closing bracket. I agree that putting the dollar sign in front of it is not the most helpful hint, but that's one of the occupational hazards with PHP :) – kalatabe Apr 08 '15 at 08:58
  • btw. mysql_close($conn); shouldn't be mysql_close($connection); ? – zdeniiik Apr 08 '15 at 09:00
  • PHP doesn't just put a dollar sign in the error message! It is because the error is somewhere else where OP has the variable `$end` which then is unexpected, because of a syntax error! – Rizier123 Apr 08 '15 at 09:00
  • Right, I just did a quick test and the proper error, at least on my PHP 5.6 is actually `syntax error, unexpected end of file in...`, seems I've confused the two, though it *may* vary between versions. – kalatabe Apr 08 '15 at 09:04
  • 1
    Hi Kaloyan, Thank you for spotting this! I feel dumb for not noticing! Thank you for the advice! – Aaron Richard Howells Apr 08 '15 at 09:04
  • *though it may vary between versions* No it does NOT! – Rizier123 Apr 08 '15 at 09:05
  • Because PHP has never in its lifetime had a history of inconsistencies :) I've made a mistake and I admitted it, fine, go easy on the capitals there. – kalatabe Apr 08 '15 at 09:07