Why do I have so many closing brackets before sql16

I have the following code and I think there are too many closing brackets but it seems to work well with it. I am not sure if my number of brackets matched the number of opening brackets or if there is anything wrong with my syntax here… Would appreciate if someone could check that for me? Especially right before sql16

<?php
ob_start();
include_once __DIR__.'/header2.php';
if ($_SESSION['u_uid']) {
   echo "<meta http-equiv='refresh' content='0;url=header2.php?index=mustloggedoutfirst'>"; 
   exit();
} else {
include_once __DIR__.'/includes/dbh.php';
date_default_timezone_set('Australia/Melbourne');
$date = strip_tags(date('Y-m-d H:i:s'));
$banned = 1;
$student_added = 'Yes';
$activate = 0;
$email_verify = 0;
$email_verify_expiry = null;
$login_attempts = null;
$login_attempts_reminder = 2;
$student_added ='No';



$sql = "SELECT * FROM users;";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
   echo 'SQL error';
   exit();
} else {
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $resultCheck = mysqli_num_rows($result);

$sql2 = "SELECT * FROM users WHERE ? >= email_verify_expiry;";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql2)) {
   echo 'SQL error';
   exit();
} else {
    mysqli_stmt_bind_param($stmt, "s", $date);
    mysqli_stmt_execute($stmt);
    $result2 = mysqli_stmt_get_result($stmt);
    $resultCheck2 = mysqli_num_rows($result2);
    $row2 = mysqli_fetch_assoc($result2);

	if ($resultCheck2 > 0) {
	    $sql3 = "UPDATE users
	             SET email_verify = ?, email_verify_expiry = ?
	             WHERE user_uid = ?
	             
	             ;";
	             
	   $stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql3)) {
   echo 'SQL error';
   exit();
} else {
    mysqli_stmt_bind_param($stmt, "iss", $email_verify, $email_verify_expiry, $row2['user_uid']);
    mysqli_stmt_execute($stmt);
    
    $sql4 = "SELECT * FROM users WHERE user_session >= ?;";
    
    
    $stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql4)) {
   echo 'SQL error';
   exit();
} else {
    mysqli_stmt_bind_param($stmt, "s", $session_expiry);
    mysqli_stmt_execute($stmt);
    $result4 = mysqli_stmt_get_result($stmt);
    $resultCheck4 = mysqli_num_rows($result4);
    
    if ($resultCheck4 > 0) {
         $sql5 = "DELETE FROM users WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql5)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row2['user_uid'], $row2['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          
                          $sql6 = "DELETE FROM memberships WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql6)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row2['user_uid'], $row2['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          $sql7 = "DELETE FROM customer_lessonfees WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql7)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row2['user_uid'], $row2['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          $sql8 = "DELETE FROM rewards WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql8)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row2['user_uid'], $row2['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          
                          
                           $sql9 = "DELETE FROM student_details WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql9)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row2['user_uid'], $row2['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                           // Edit this path if PHPMailer is in a different location.


//$mail->SMTPDebug = 2;
include_once __DIR__.'/PHPMailer/Exception.php';
include_once __DIR__.'/PHPMailer/PHPMailer.php';
include_once __DIR__.'/PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->CharSet = 'UTF-8';
//$mail->isSMTP();  enable this for localhost and disable for live host??? 
$mail->isHTML(true);

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS and 465 for SSL security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "[email protected]"; // Your Gmail address.
$mail->Password = "******"; // Your Gmail login password or App Specific Password.

/*
 * Message Configuration
 */

$mail->setFrom('[email protected]', 'PianoCourse101 Account Deleted'); // Set the sender of the message.
$mail->addAddress($row2['user_email'], 'PianoCourse101 Account Deleted'); // Set the recipient of the message.
$mail->Subject = 'PianoCourse101 Account Deleted'; // The subject of the message.

/*
 * Message Content - Choose simple text or HTML email
 */
 
// Choose to send either a simple text email...
$mail->Body = "Dear ".$row2['user_first']." ".$row2['user_last'].", <br /></br />

You have received this e-mail because your Primer Level Account has been deleted. You have not login for more than 1 month.

<br /><br />

From,

<br /><br />

PianoCourse101
"; // Set a plain text body.

// ... or send an email with HTML.
//$mail->msgHTML(file_get_contents('contents.html'));
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file
$mail->addAttachment('images/phpmailer_mini.png');
if ($mail->send()) {
  //  echo "<meta http-equiv='refresh' content='0;url=index.php?delete_request=success'>";
  //          exit();
                          
                  	 
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
                                
} 
}
}
}
}
}else {
   
   $sql10 = "SELECT * FROM users WHERE email_verify = ?;";
    
    $stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql10)) {
   echo 'SQL error';
   exit();
} else {
    mysqli_stmt_bind_param($stmt, "i", $email_verify);
    mysqli_stmt_execute($stmt);
    
    $result10 = mysqli_stmt_get_result($stmt);
    $resultCheck10 = mysqli_num_rows($result10);
    $row10 = mysqli_fetch_assoc($result10);
    
    if ($resultCheck10 > 0) {
        $sql11 = "DELETE FROM users WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql11)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row10['user_uid'], $row10['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          
                          $sql12 = "DELETE FROM memberships WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql12)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row10['user_uid'], $row10['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          $sql13 = "DELETE FROM customer_lessonfees WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql13)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row10['user_uid'], $row10['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          $sql14 = "DELETE FROM rewards WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql14)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row10['user_uid'], $row10['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                          
                          
                           $sql15 = "DELETE FROM student_details WHERE user_uid = ? AND user_email = ?;";
                                
                                 $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql15)) {
                           echo "SQL error";
            } else {
                          mysqli_stmt_bind_param($stmt, "ss", $row10['user_uid'], $row10['user_email']);
                          mysqli_stmt_execute($stmt);
                          
                           // Edit this path if PHPMailer is in a different location.


//$mail->SMTPDebug = 2;
include_once __DIR__.'/PHPMailer/Exception.php';
include_once __DIR__.'/PHPMailer/PHPMailer.php';
include_once __DIR__.'/PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->CharSet = 'UTF-8';
//$mail->isSMTP();  enable this for localhost and disable for live host??? 
$mail->isHTML(true);

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS and 465 for SSL security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "[email protected]"; // Your Gmail address.
$mail->Password = "********"; // Your Gmail login password or App Specific Password.

/*
 * Message Configuration
 */

$mail->setFrom('[email protected]', 'PianoCourse101 Account Deleted'); // Set the sender of the message.
$mail->addAddress($row2['user_email'], 'PianoCourse101 Account Deleted'); // Set the recipient of the message.
$mail->Subject = 'PianoCourse101 Account Deleted'; // The subject of the message.

/*
 * Message Content - Choose simple text or HTML email
 */
 
// Choose to send either a simple text email...
$mail->Body = "Dear ".$row2['user_first']." ".$row2['user_last'].", <br /></br />

You have received this e-mail because your Primer Level Account has been deleted. You have not verified your e-mail account within 24 hours by activating your account.

<br /><br />

From,

<br /><br />

PianoCourse101
"; // Set a plain text body.

// ... or send an email with HTML.
//$mail->msgHTML(file_get_contents('contents.html'));
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file
$mail->addAttachment('images/phpmailer_mini.png');
if ($mail->send()) {
//    echo "<meta http-equiv='refresh' content='0;url=index.php?delete_request=success'>";
  //          exit();
                          
                  	 
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}        
                                
    }
	}         
}
}
    } 
	}
	}
	}
	}
	}
	}else {
	    $sql16 = "SELECT * FROM users WHERE student_added IS NULL;";
	    
	       $stmt = mysqli_stmt_init($conn);
            if(!mysqli_stmt_prepare($stmt, $sql16)) {
                           echo "SQL error";
            } else {
                          
                          mysqli_stmt_execute($stmt);
                          
                          $result16 = mysqli_stmt_get_result($stmt);
                          $resultCheck16 = mysqli_num_rows($result16);
                          
                          $row16 = mysqli_fetch_assoc($result16);
                          
                          if ($resultCheck16 > 0) {
                                // Edit this path if PHPMailer is in a different location.
                     

//$mail->SMTPDebug = 2;
include_once __DIR__.'/PHPMailer/Exception.php';
include_once __DIR__.'/PHPMailer/PHPMailer.php';
include_once __DIR__.'/PHPMailer/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->CharSet = 'UTF-8';
//$mail->isSMTP();  enable this for localhost and disable for live host??? 
$mail->isHTML(true);

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS and 465 for SSL security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "[email protected]"; // Your Gmail address.
$mail->Password = "********"; // Your Gmail login password or App Specific Password.

/*
 * Message Configuration
 */

$mail->setFrom('[email protected]', 'PianoCourse101 Add Student Reminder'); // Set the sender of the message.
$mail->addAddress($row16['user_email'], 'PianoCourse101 Add Student Reminder'); // Set the recipient of the message.
$mail->Subject = 'PianoCourse101 Add Student Reminder'; // The subject of the message.

/*
 * Message Content - Choose simple text or HTML email
 */
 
// Choose to send either a simple text email...
$mail->Body = "Dear ".$row16['user_first']." ".$row16['user_last'].", <br /></br />

You have received this e-mail because you have not added at least one student yet!

<br /><br />

In order to use PianoCourse101, you must at least add one student so that you can use your practice diary.

<br /><br />

From,

<br /><br />

PianoCourse101
"; // Set a plain text body.

// ... or send an email with HTML.
//$mail->msgHTML(file_get_contents('contents.html'));
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file
$mail->addAttachment('images/phpmailer_mini.png');
if ($mail->send()) {
//    echo "<meta http-equiv='refresh' content='0;url=index.php?delete_request=success'>";
  //          exit();
                          
                  	 
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}        
                                
                          }
	}
}
            }
            }
}            
	
                                 
ob_end_flush();




?>

If I tried to reduce the brackets before sql 16, it will mention something about unexpected t_else error… why am I getting this error?

oooooh. before sql16 it’s a lot. i think it be cool to run this code. then remove extra bracelets on the line which php throws it as error

t_else means there’s an extra starter bracelet

so does that mean that there is a problem with my previous brackets?

yes

Could you try to get this to work for me with less brackets? I am confused

mmmm… okay… let me see

i’m getting confused myself. but i’ll try it.i’m wondering the error points to which line?

well… not sure of the line but just before sql16… I keep getting that error but I am sure that all my brackets before are correct… unless, am I meant to close the bracket for each sql statement?

Which of the following is correct? option a or b?

option a)

$sql = “SELECT * FROM users;”;

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
echo ‘SQL error’;
exit();
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);

$sql2 = “SELECT * FROM users WHERE ? >= email_verify_expiry;”;

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo ‘SQL error’;
exit();
} else {
mysqli_stmt_bind_param($stmt, “s”, $date);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);

option b)

$sql = “SELECT * FROM users;”;

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
echo ‘SQL error’;
exit();
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
}
$sql2 = “SELECT * FROM users WHERE ? >= email_verify_expiry;”;

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql2)) {
echo ‘SQL error’;
exit();
} else {
mysqli_stmt_bind_param($stmt, “s”, $date);
mysqli_stmt_execute($stmt);
$result2 = mysqli_stmt_get_result($stmt);
$resultCheck2 = mysqli_num_rows($result2);

Should I have that closing bracket before the $sql2?

i prefer option a). cuz on option b) closing bracelet before sql2 wont allow it to run well i bet

1 Like

This is confusing because I read somewhere that option 2 is better…

i was trying similar to option a) on my site. so neither is bad

1 Like

but currently, I am also using option a, so will stick with that… but have you figured out the brackets issue?

How is it going with the test of brackets?

As to why you have so many brackets, I put your code in my editor and hit the auto format button. This is what the bracket situation turned into:

Scrolling through the code, I suspect it has to do with the fact that the entire file is structured like:

  • Run some code.
  • Have an if statement.
  • If the if statement is true, print stuff and exit.
  • In the else statement, put all the other code of the entire file.

What will help reduce some of this depth is to skip the else statement and put your code on the same level as the code before. So instead of:

<?php
ob_start();
include_once __DIR__ . '/header2.php';
if ($_SESSION['u_uid']) {
    echo "something";
    exit();
} else {
    // all other code
}

Do:

<?php
ob_start();
include_once __DIR__ . '/header2.php';
if ($_SESSION['u_uid']) {
    echo "something";
    exit();
}
// all other code

With the former, you get the situation where you can count how many SQL queries have already been performed by the number of closing brackets (because most of the if statements are SQL error handling).

Also, general tip: learn how and when to use functions.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.