hi guys , i wrote this code but when i try to open the page it says there is a syntax error on line 26
here is the code :

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>
<strong><h2>Welcome to Our Website</h2></strong>
</td>
</tr>
</table>
<br />
<?
$tbl_name = "posts";
mysql_connect('localhost','username','password')or die("can not connnect to database");
mysql_select_db("$musql_database")or die("sorry , can not sellect db");
$sql="SELECT*FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array("$result"))
{
?>
<table width="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr><td>
<table width="400" border="0" cellpadding="2" cellspacing="1" bgcolor="#ffffff">
<tr>
<td>Title : <? echo $rows['title']; ?></td>
</tr>
<tr>
<td>--> <?   echo $rows['post']; ?> </td>
</tr>
</table></td></tr></table>

and this is the error :
Parse error: syntax error, unexpected $end in /www/a/w/a/awah/htdocs/add_email_test/admin/view-posts.php on line 26

this code is to get the post and the title form the database and it view it ,like a blog .

Dani AI

Generated

A short diagnosis and practical steps that complement the replies from , , , and .

The parser message "unexpected $end" means PHP reached the end of the file while still expecting a closing token (brace, string/here-doc terminator, or a PHP block). Several posts already pointed to likely causes; the checklist below shows reliable ways to locate the exact problem and avoid similar mistakes going forward.

  • Quick syntax check from the command line:

    php -l /path/to/view-posts.php

    This linter reports the first syntax error and a line number. Use it first — it often gives a clearer location than the web error.

  • Make PHP show all errors while debugging:

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

    Add that temporarily at the top of the script to reveal hidden parse/runtime issues.

  • Systematic isolation: create a minimal copy of the file and strip out large HTML blocks. Re-introduce sections until the parser fails again. This narrows the problematic region quickly.

  • Editor and file checks: use an editor with bracket/brace matching and search for unclosed strings or heredocs. Save files as "UTF-8 without BOM" (a BOM at the start can confuse some parsers). If you rely on short tags, confirm the server setting or use long <?php tags.

  • After syntax fixes, add defensive checks for database calls so failures show useful errors:

    if ($result === false) {
        die('Query error: ' . mysql_error());
    }
  • Long term: avoid the deprecated mysql_* extension. Move to mysqli or PDO and use prepared statements for security and forward compatibility (Choosing an API). For PHP error handling reference, see error_reporting.

These steps will locate the parse error fast and improve stability. Once the syntax issue is resolved, follow up by sanitizing inputs and migrating to a supported DB API.

Recommended Answers

All 5 Replies

is that the whole code?
in which case you must start with php tags rather than half way down.
just put <?PHP //start php ?>
at the top with no spaces in front.
also your server may not support short tags <? ?> change to <?PHP and ?>
line 12 you have musql instead of mysql, this could be a possible typo
line 13 try putting spaces in your query "SELECT * FROM $tbl_name"


oh actually i read your code properly now and you've forgotten to close your while statement.
put <?PHP } ?> before your end table :)
hope this helps

Member Avatar for Member #334542
while($rows=mysql_fetch_array("$result"))
{

Missed out the closing brace.

Close the while statement at the end of the table
<?php
}
?>
and avoide using <?//code?> instead try to use <?php //code /?>
cause it may cause some serious problem in some hosts, if allow <? ?> is not un commented in your php.ini file

1.sql command select*from >> select * from
2. while($rows=mysql_fetch_array("$result")) >> while($rows=mysql_fetch_array($result))
3. you forget close end for while
try to use <?php } ?> andtheend of html tags.

Finally Try with this code :

<?
$musql_database="dbname"; // Give Your DB name also
$tbl_name = "posts";
mysql_connect('localhost','username','password')or die("can not connnect to database");
mysql_select_db("$musql_database")or die("sorry , can not sellect db");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

?><table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>
<strong><h2>Welcome to Our Website</h2></strong>
</td>
</tr>
</table>
<br />
<?
while($rows=mysql_fetch_array($result))
{
?>
<table width="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr><td>
<table width="400" border="0" cellpadding="2" cellspacing="1" bgcolor="#ffffff">
<tr>
<td>Title : <? echo $rows['title']; ?></td>
</tr>
<tr>
<td>--> <?   echo $rows['post']; ?> </td>
</tr>
</table></td></tr></table>
<? }?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.