Parse error: syntax error, unexpected $end in file on line 128
The error message Parse error: syntax error, unexpected $end is one of the most common PHP pitfalls, especially when mixing HTML and PHP logic. It indicates that the PHP parser reached the end of a file while still expecting more tokens to complete a statement or close a block. In your specific case, the issue isn't that the database logic fails; it's that the code inside the <body> tag is malformed in a way that breaks the PHP execution context before it finishes parsing the HTML structure.
Looking closely at your source code, the primary culprit is the incomplete if statement wrapping the main dropdown form and table output. You have an opening brace { after mysql_select_db($dbname, $conn), but you never close it with a corresponding } before the PHP tags end or the HTML structure continues.
Here is the problematic section from your code:
if (mysql_select_db($dbname, $conn))
{
?>
<form method="post" action="new 2.php">
<!-- ... rest of form and table logic ... -->
}
?>
Notice the closing ?> at the very end of the PHP block. While it closes the statement logically, the parser expects the code inside that if block to be properly terminated. However, the real issue often stems from how the tags are nested or if there is a missing closing brace for the if condition before the final ?>.
In your snippet, the logic flows like this:
- Connect to MySQL.
- Run
SHOW TABLES. - Check if the database exists using
mysql_select_db. - If true (inside
{ ... }), output the form and table.
The parser hits an error because the block inside the if statement contains multiple PHP sections mixed with HTML. Specifically, look at this sequence near the end of your script:
} // Closing the if? Or is it missing?
?>
<div>...</div>
</body>
</html>
If you count the opening and closing braces for the if (mysql_select_db) block, you will likely find a mismatch. The parser reads everything until it sees ?>. If the } that closes the if block is missing or placed incorrectly relative to the ?>, PHP thinks the file ended prematurely inside that conditional block.
Fixing the Syntax Error
To resolve this, you must ensure that every opening brace { has a matching closing brace } before the final ?>. In your case, the structure should look like this:
if (mysql_select_db($dbname, $conn)) {
?>
<form method="post" action="new 2.php">
<select name="tables">
<?php
while ($row = mysql_fetch_row($result)) {
echo '<option value="'.$row[0].'">'.$row[0].'</option>';
}
?>
</select>
<!-- ... -->
</form>
<?php
if (isset($_POST['tables'])) {
$tbl = $_POST['tables'];
$query = "SELECT * FROM $tbl";
$res = mysql_query($query);
if ($res) {
?>
<table border="1">
<?php
while ($row = mysql_fetch_array($res)) {
echo "<tr>";
// ...
}
?>
</table>
<?php
}
}
?>
</div>
Critical Security and Architecture Issues
Beyond the immediate syntax error, your code relies on two deprecated functions: mysql_connect and mysql_query. These were deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0. If you are running PHP 7 or higher (which is standard for most modern hosting environments today), your script will not execute at all, regardless of the syntax fix.
You need to migrate to MySQLi or PDO. PDO is generally recommended for Laravel projects due to its flexibility and integration with dependency injection.
Here is how you would refactor the connection logic using PDO prepared statements:
try {
// Replace 'snysbarchive' with your actual database name
$pdo = new PDO("mysql:host=localhost;dbname=snysbarchive;charset=utf8", "root", "usbw");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Fetch tables safely
$stmt = $pdo->query("SHOW TABLES");
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
echo '<form method="post">';
echo '<select name="tables">';
foreach ($tables as $table) {
echo '<option value="' . htmlspecialchars($table) . '">' . htmlspecialchars($table) . '</option>';
}
echo '</select><input type="submit" value="Show">';
echo '</form>';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$table = $_POST['tables'];
// Using a prepared statement is impossible for the table name directly,
// but we can at least sanitize or validate the name against a whitelist.
// For production, always build a list of allowed tables and check against it.
$stmt = $pdo->query("SELECT * FROM `$table`");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<table border="1">';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Why This Matters
Using deprecated functions like mysql_* not only risks a syntax error in newer PHP versions but also exposes your application to SQL injection vulnerabilities. Your current code concatenates user input directly into queries ("SELECT * from $tbl"). An attacker could enter '; DROP TABLE users; -- into the dropdown and execute arbitrary commands.
If you are building this for a modern project, consider migrating to Laravel. Laravel provides robust database abstraction, built-in protection against SQL injection via Eloquent ORM or Query Builder, and handles connection pooling efficiently. For instance, in Laravel, you would define your models and use DB::table()->get() which automatically sanitizes inputs.
By fixing the brace mismatch and upgrading to PDO or a modern framework, you eliminate the parse error and secure your application against common injection attacks.