PHP “Parse Error: Syntax Error, Unexpected ‘;'” in a Signup Class: One Missing Bracket, and Two More Bugs Waiting Behind It

Line 16, as written and as it should be

if (!$stmt->execute(array($uid,$email,$hashedPwd) {
if (!$stmt->execute(array($uid,$email,$hashedPwd))) {

if (execute(array(  3 openvs)) 2 closed

Line 16 opens three brackets and closes only two. PHP is still reading the if condition when it reaches the semicolon on line 17, and that is why the error names line 17 rather than the line with the mistake. Adding the missing ) clears the parse error, though the function still does not work: $stmt was never created, and the values go into the wrong columns. All three faults are fixed in the class further down, and each step was run on PHP 8.3 to show the message you would see next.

Read the Error the Way PHP Reads Your Code

PHP · the code from the question, with line numbers as PHP counts them
10 protected function setUser($uid, $pwd, $email)
11 {
12 $this->connect()->prepare('INSERT INTO users ( users_uid , users_pwd , users_email) VALUES ( ? , ? , ? ) ');
13
14 $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
15
16 if (!$stmt->execute(array($uid,$email,$hashedPwd)){
17 $stmt = null ; header("location: ../index.php?error=stmtfailed") ; exit();
18 } }
php -l signup.classes.php
PHP Parse error: syntax error, unexpected token ";" in signup.classes.php on line 17

PHP reads from left to right and keeps count of open brackets. On line 16 it opens one for if, one for execute and one for array. Two close before the curly brace, so the if condition is still open. PHP then tries to read { $stmt = null as part of that condition, and the first ; on line 17 is the point where nothing it knows can continue. So the line in a parse error is where PHP gave up, which can be a line or two after the real mistake. Look at the line above it first.

  • Count brackets from the left: add one for each (, take one away for each ). The count must be 0 before the {.
  • Let the editor count for you: VS Code colours matching brackets in pairs, and clicking next to one highlights its partner.

The Missing Bracket Was Hiding an Undefined Variable

PHP · after adding only the missing bracket
if (!$stmt->execute(array($uid,$email,$hashedPwd))) {
PHP Warning:  Undefined variable $stmt in signup.classes.php on line 16
PHP Fatal error:  Uncaught Error: Call to a member function execute() on null in signup.classes.php:16

Line 12 prepares the query, but the result is thrown away, because nothing is written before $this->connect(). On line 16, $stmt does not exist, so PHP warns about the undefined variable and then stops with a fatal error, since there is nothing to call execute() on. The fix is four characters: $stmt = at the start of line 12.

Then the Signup Works and Saves the Wrong Data

PHP · after storing the prepared statement in $stmt
$stmt = $this->connect()->prepare('INSERT INTO users (users_uid, users_pwd, users_email) VALUES (?, ?, ?)');
What ended up in the users table
users_uid | users_pwd | users_email
ash | ash@example.com | $2y$10$PunY7DmwU5M.M...

This one prints no error at all, which makes it the worst of the three. The query lists the columns as uid, pwd, email, but the array passes uid, email, hashedPwd. The question marks are filled in order, so the email lands in the password column and the hash lands in the email column. Login will then fail for every user, because password_verify() will be checking the password against an email address.

The values in execute() must be in the same order as the columns in the query.

Here Is the Corrected Class

PHP · signup.classes.php (replace the whole file)

<?php

class Signup extends Dbh
{
    protected function setUser($uid, $pwd, $email)
    {
        $stmt = $this->connect()->prepare(
            'INSERT INTO users (users_uid, users_pwd, users_email) VALUES (?, ?, ?)'
        );

        $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

        try {
            $stmt->execute([$uid, $hashedPwd, $email]);
        } catch (PDOException $e) {
            error_log('Signup failed: ' . $e->getMessage());
            $stmt = null;
            header('Location: ../index.php?error=stmtfailed');
            exit();
        }

        $stmt = null;
    }
}
  • $stmt = keeps the prepared statement so it can be executed.
  • [$uid, $hashedPwd, $email] matches the column order: uid, pwd, email.
  • The brackets balance because the if is replaced by a try block. There is a reason for that, explained next.
  • The real database message goes to the error log with error_log(), while the visitor is sent back with ?error=stmtfailed as before.

Why Try and Catch Instead of If

Since PHP 8.0, PDO throws an exception when a query fails, unless you switch that off. execute() never gets the chance to return false, so the old if (!$stmt->execute(...)) branch would never run and the visitor would see a fatal error page. catch (PDOException $e) is where that failure arrives now. We tested it by signing up the same username twice on a table where the username is unique:

PHP error log · the same username signed up a second time
Signup failed: SQLSTATE[23000]: Integrity constraint violation: UNIQUE constraint failed: users.users_uid

The visitor was sent back to the form, and the cause was written to the log.

The Connection Class to Go With It

Your Signup class extends a Dbh class from the same tutorial. Make sure it sets the error mode so the behaviour above is the same on every PHP version. On AMPPS the default MySQL user is root with the password mysql; change both for a live site.

PHP · dbh.classes.php (the connection class the signup class extends)
<?php
class Dbh
{
protected function connect()
{
$pdo = new PDO('mysql:host=localhost;dbname=projectxxx;charset=utf8mb4', 'root', 'mysql');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
}

What the Table Looks Like When It Works

What the table holds after a signup with the fixed class
users_uid | users_pwd | users_email
ash | $2y$10$J0h9Hx.W8Cfgi... | ash@example.com

password_verify('secret123', $row['users_pwd']) -> bool(true)

The password column holds a hash that starts with $2y$, and password_verify() confirms the original password against it. That is exactly what the login side of the tutorial will check.

Catch the Next Parse Error Before You Open the Browser

PHP can check a file for syntax errors without running it. Run this in the terminal after editing a class, and the bracket mistake shows up in a second instead of as a white page.

Terminal · check a file for syntax errors without running it
php -l classes/signup.classes.php
What you should see
No syntax errors detected in classes/signup.classes.php

If php is not recognised on Windows, AMPPS keeps its PHP in its own folder, for example C:\Program Files\Ampps\php\php.exe. Use that full path, or add the folder to PATH.

How These Fixes Were Checked

The code from the question was saved with the same line numbers and checked with php -l on PHP 8.3. That reproduced the exact message from the question, line 17 included. Each fix was then applied one at a time and run against a small SQLite users table, which is why the messages and table rows above are real output. On MySQL the duplicate-username message reads slightly differently (Duplicate entry 'ash' for key), but it arrives in the same catch block.

Related blog posts