Click here to Skip to main content
16,013,824 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<td>
        <h4>To view discounts, simply select from a category below.</h4><br />
        <!-- List Category Box -->
 
        <?php
        $db = new mysqli('50.62.209.81:3306','user','password','MassWarVetsADM');
        $query="SELECT Category FROM Category";
        $result = mysql_query ($query);
        echo "<select name='categoriesSelect' class='e1'><option value='0'>Please Select A Category</option>";
 
        // printing the list box select command
        while($categoryinfo=mysql_fetch_array($result)){//Array or records stored in $nt
        echo "<option value=\"".htmlspecialchars($categoryinfo['category'])."\">".$categoryinfo['category']."</option>";
        }
        echo"</select>";
        ?>


When I look at my page in firefox/IE/chrome I get this: http://tinypic.com/r/2eqc22v/8[^]

I removed the username&pw for security reasons. What am I doing wrong?

This should print inside the select box the list of 35 or so categories listed in the Category column of the Category table inside my database
Posted
Updated 29-Jul-14 14:24pm
v3

1 solution

This is object oriented style:
PHP
$db = new mysqli('50.62.209.81:3306','user','password','MassWarVetsADM');

This is procedural style:
PHP
$result = mysql_query ($query);

i) don't mix these two styles
ii) don't use mysql_query, cause it's deprecated
iii) htmlspecialchars() is probably not the desired decoding.

For example you could make a query like the following:
PHP
$db = new mysqli('50.62.209.81:3306','user','password','MassWarVetsADM');
$query="SELECT Category FROM Category";
if ($result = $mysqli->query($query)){
   echo "<select name="categoriesSelect" class="e1">";
   echo "<option value="0">Please Select A Category</option>";
   while($categoryinfo = $result->fetch_assoc()){
      $sCategory = $categoryinfo['category'];
      echo "<option value="\"".$sCategory" .="\">".$sCategory ."</option>";
   }
   echo "</select>";
}else{
   echo "No items to select.";
}

The above example you could expand with try/catch and more...

You find more details about using the mysqli-extension on:
mysqli-class
mysqli-query
mysqli-result-class
mysqli-fetch_assoc
 
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