-1

Apologies if this is a very frequently asked question in here but this is really throwing me off as I can't seem to find where my syntax error is:

<?php require "../connect.php";

$sql = "SELECT pro_exp_date, pro_exp_descr FROM pro_exp";
$result = mysqli_query($connect,$sql);

if ($result->num_rows > 0) :
    while ($row = mysqli_fetch_assoc($result)) :
        echo "<dt class='col-md-3'>" . $row["pro_exp_date"] . "</dt><dd class='col-md-9'><p>" . $row["pro_exp_descr"] . "</p></dd>";
    else :
        echo "0 results.";

...

The error seems to begin in line echo "<dt..., and this is at the start of the page, before there is no semicolons missing or anything like that. Another user in here suggests to use non-bracketed syntax for easier coding, so I did just that. I have changed ' to " and vice versa in and out of the HTML, nothing works.

ADD: I added endwhile and endif and that seems to work, no errors — but I tried reverting back to normal bracket syntax and it threw the error again. I even retraced and rewrote the brackets one by one to make sure everything fits, but that doesn't seem to work. endwhile and endif works just fine.

ADD 2:

This works fine:

if ($result->num_rows > 0) :
    while ($row = mysqli_fetch_assoc($result)) :
        echo "<dt class='col-md-3'>" . $row["pro_exp_date"] . "</dt><dd class='col-md-9'><p>" . $row["pro_exp_descr"] . "</p></dd>";
    endwhile;
else :
        echo "0 results.";
endif;

This doesn't:

if ($result->num_rows > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<dt class='col-md-3'>" . $row["pro_exp_date"] . "</dt><dd class='col-md-9'><p>" . $row["pro_exp_descr"] . "</p></dd>";
    } else {
        echo "0 results.";
}

I don't know why. But the endif and endwhile works okay.

jonvyltra
  • 111
  • 3
  • 1
    where is the while closing? – Kevin May 28 '20 at 02:35
  • 1
    Unless you're using PHP like a template language, I wouldn't recommend the alt syntax. Just use braces, ie `while(...) { ... }` – Phil May 28 '20 at 02:38
  • Before I tried the alt syntax, the closing } bracket was exactly before the ```else``` — this is really throwing me off, but I'll try ending the alt syntax first – jonvyltra May 28 '20 at 02:39
  • If you're using alt syntax, you must have an `endwhile` and `endif` – Phil May 28 '20 at 02:40
  • isn't using that syntax harder to use, at the very least most text editors honor `{}` and even highlights the opening and closing, easier on the eyes – Kevin May 28 '20 at 02:42
  • @Kevin that depends. Personally, I've found getting PHP to `echo` great chunks of mostly static HTML is unnecessary and bound to run into issues with string quotes – Phil May 28 '20 at 02:49
  • _"I tried reverting back to normal bracket syntax and it threw the error again"_ if you want to get to the bottom of this, please show what you tried and show exactly what the error message says – Phil May 28 '20 at 02:53
  • @Phil - updated again. The error is an unexpected else right at the `while`'s closing bracket. – jonvyltra May 28 '20 at 02:58
  • You haven't close the `if` block. The syntax for `if..else` is `if { ... } else { ... }` where the `else` comes after the `if` block, not within it – Phil May 28 '20 at 02:59
  • @Phil ah yes, that's the solution. Hope this thread can help other rookies like me, really basic mistake. Solved then! – jonvyltra May 28 '20 at 03:04

2 Answers2

0

You need to close your while and if statements:

if ($result->num_rows > 0) :
    while ($row = mysqli_fetch_assoc($result)) :
        echo "<dt class='col-md-3'>" . $row["pro_exp_date"] . "</dt><dd class='col-md-9'><p>" . $row["pro_exp_descr"] . "</p></dd>";
    endwhile;
else :
    echo "0 results.";
endif;

My suggestion is to revert back to brackets ({ and }).

Marco
  • 7,007
  • 2
  • 19
  • 49
0

If you're going to use alternative syntax for control structures, it fits best when used as a sort of template language.

For example

$result = mysqli_query($connect,$sql);

if ($result->num_rows > 0) :
    while ($row = mysqli_fetch_assoc($result)) : ?>
        <dt class="col-md-3"><?= htmlspecialchars($row['pro_exp_date']) ?></dt>
        <dd class="col-md-9">
            <p><?= htmlspecialchars($row['pro_exp_descr']) ?></p>
        </dd>
    <?php endwhile; 
else : ?>
    0 results
<?php endif ?>
Phil
  • 157,677
  • 23
  • 242
  • 245