Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently working on a page that loads items from my database into a dropdown (option) menu.

My code:
XML
<html>
<?php

$con=mysqli_connect("localhost","root","","mysql");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"mysql");

$query = "SELECT * FROM artikel";
$rs1 = mysql_fetch_array($con,$query);
?>

            <select>
            <option>Selecteer een artikel:</option>
            <?php

            foreach ($rs1 as $row) {
              echo '<option value=\"{$row['Omschrijving']}\">{$row['Omschrijving']}</option>';
            }
            ?>
            </select>

</html>


Error:

Parse error: syntax error, unexpected 'Omschrijving' (T_STRING), expecting ',' or ';' in D:\Program Files\xampp\htdocs\Week 5\opgave 1.9.1.php on line 20


Anyone any idea what's wrong with my echo?
Posted

1 solution

The reason you're getting the error, is because you are not building your echo statement correctly.

Instead of doing...
PHP
echo '<option value=\"{$row['Omschrijving']}\">{$row['Omschrijving']}</option>';


You should do:

PHP
echo '<option value="{' . $row['Omschrijving'] . '}">{' . $row['Omschrijving'] . '}</option>';


The server will then parse the echo statement to html like follows:

HTML
<option value="{whatever value}">{whatever value}</option>


Hope that helps!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900