0

The assignment is: Change your fruit order form to construct the table of names, prices and weights by extracting the items from the database rather than hard-coding them. Display every item from the database. Do not assume that you know the number of items. I am having problems because of something in my if statement or it might be my while loop. I don't know exactly what I am doing wrong and I need some help trying to figure this out.

    <table>
        <th>Fruits For Sale!</th>
        <tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
         <?php
        $db=mysqli_connect(null,null,null,'weblab')
        or die("Can't connect to DB:" . mysqli_connect_error());
        $q = "select fruit_item_no, fruit_name, fruit_weight, fruit_price";
        $q .= "from fruit_t";
        $q .= "order by fruit_name;";
        $dbResult = mysqli_query($db,$q)
        if ($num == 0) {
         echo '<tr><td colspan="2">';
        echo 'Database query retrieved zero rows.</td></tr>';
}
 while ($row = mysqli_fetch_assoc($dbResult)) {
        $name = $row['fruit_name'];
        $weight = $row['fruit_weight'];
        $price = $row['fruit_price'];
        echo "<tr><td><b>$name</b></td>";
        echo "<td>$weight</td>";
        echo "<td>$price</td></tr>\n";
}
?>
</table>

3 Answers3

2

you're getting an unexpected if because the previous line believes it has not finished, you forgot the semi-colon. Change this:

$dbResult = mysqli_query($db,$q)

to:

$dbResult = mysqli_query($db,$q);
iam-decoder
  • 2,554
  • 1
  • 13
  • 28
1

Check each statement for a semi-colon. Unexpected token errors might indicate you've missed a semi-colon, check the previous statement from the line the the error message indicates.

Also, if you're going to split your SQL statement into multiple strings and then concatenate them (why?), be sure you have spaces where you need them: PHP does not join concatenated literals with spaces, it just smooshes them together.

For example:

<?php
$text = 'SELECT *';
$text .= 'FROM table';
$text .= 'WHERE 1';
echo $text;
// $text is equal to the following:
// 'SELECT *FROM tableWHERE 1'
AVProgrammer
  • 1,344
  • 2
  • 20
  • 40
1

at a glance, you should leave spaces between the parts of the sql statement

$q .= " from fruit_t";
$q .= " order by fruit_name;";
mplexus
  • 39
  • 1
  • 8