Q: Parsing unclosed `}` tags enter image description here
I want to use html code between foreach, but I get this error about not closing the tag (})

error Message :
( ! ) Parse error: Unclosed '{' on line 26 in D:\xampp\htdocs\php\php_lernen\myphp\databasepdo.php on line 52

does anyone maybe know about this error message? Here is the Code:
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<table>
    <tr>
        <th>id</th>
        <th>user name</th>
        <th>name</th>
        <th>email</th>
    </tr>
    <?php
    $servername = 'localhost';
    $dbname = 'test';
    $username = 'root';
    $password = '';
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM user";
        $results = $conn->query($sql);
        foreach ($results as $results) {
            ?>
            <tr>
                <td><?php echo $results['id'] ?></td>
                <td><?php echo $results['username'] ?></td>
                <td><?php echo $results['name'] ?></td>
                <td><?php echo $results['mail'] ?></td>
            </tr>
            <?
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    $conn = null;
    ?>
</table>
</body>
</html>


A: You are missing the php tag on line 36. Try this:
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM user";
    $results = $conn->query($sql);
    foreach ($results as $results) {
        ?>
        <tr>
            <td><?php echo $results['id'] ?></td>
            <td><?php echo $results['username'] ?></td>
            <td><?php echo $results['name'] ?></td>
            <td><?php echo $results['mail'] ?></td>
        </tr>
        <?php // <------ Missing php tag here
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}


A: You need a ';' right after you come back into your php code, the <? before your closing bracket }.
When you exit your php to insert some html in that particular case or loop iteration, it is still a 'line' of code as far as your PHP goes and needs to be ended appropriately, thus ';'. Here is that portion of code fixed:
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM user";
        $results = $conn->query($sql);
        foreach ($results as $results) {
            ?>
            <tr>
                <td><?php echo $results['id'] ?></td>
                <td><?php echo $results['username'] ?></td>
                <td><?php echo $results['name'] ?></td>
                <td><?php echo $results['mail'] ?></td>
            </tr>
            <?; # <---- this ; here
        }

You also want to stick with <?php in these cases as I found in my own personal php that not including the entire opening tag caused my script to become unaware of the start and stop of my php code sections.
