This question already has an answer here:
I am trying to count the number of rows I am selecting from the database and write a Foreach loop if the number of rows is greater than 1. I keep getting an unexpected ')' error. Why is this?
<?php
if(isset($_SESSION['login_user'])) {
$login_session = $_SESSION['login_user'];
echo '<h1>Welcome '. $login_session .'</h1>';
}
echo '<table>';
$sql_query = "SELECT * FROM Courses";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count > 1) {
foreach($result as $row)
echo '<tr>'. $row['Title'] .'</tr>';
}
}
echo '</table>';
?>
The exact error is:
[12-Aug-2016 09:54:29 Europe/London] PHP Parse error: syntax error, unexpected '}' in /home/etd/public_html/etd/single/courses.php on line 33
</div>
It's unexpected '}'
error (curly braces).
Your foreach loop is missing the opening curly brace. It should be like this:
<?php
if(isset($_SESSION['login_user'])) {
$login_session = $_SESSION['login_user'];
echo '<h1>Welcome '. $login_session .'</h1>';
}
echo '<table>';
$sql_query = "SELECT * FROM Courses";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count > 1) {
foreach($result as $row) { // this is where the curly bracket opening is missing
echo '<tr>'. $row['Title'] .'</tr>';
}
}
echo '</table>';
?>
Without the opening there is one extra closing curly brace and as the interpreter told you - it is unexpected.
You're missing a opening curly bracket ({
) on your foreach
Your code:
if($count > 1) {
foreach($result as $row)
echo '<tr>'. $row['Title'] .'</tr>';
}
}
Should be:
if($count > 1) {
foreach($result as $row) {
echo '<tr>'. $row['Title'] .'</tr>';
}
}
The reason it's an unexpected }
because there isn't an opening bracket for it to match against
Missing braces foreachloop
<?php
if(isset($_SESSION['login_user'])) {
$login_session = $_SESSION['login_user'];
echo '<h1>Welcome '. $login_session .'</h1>';
}
echo '<table>';
$sql_query = "SELECT * FROM Courses";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count > 1) {
foreach($result as $row) {
echo '<tr>'. $row['Title'] .'</tr>';
}
}
echo '</table>';
?>