Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
<?php
        $con = mysqli_connect('server','username','password','DBName');
        // Check connection
        if (mysqli_connect_errno())
           {
           echo "<option>Failed to connect to the DB</option>" . mysqli_connect_error();
           }
         mysqli_query($con,"SELECT Category FROM Category");
         
         // HERE IS WHERE I NEED TO STORE THE VALUES & CREATE THE WHILE LOOP 
         // PRODUCING OPTION TAGS & VALUES BASED UPON THE RESULTS OF THE QUERY


         mysqli_close($con);
?>


Can someone help me complete this. My database.table only has one column "Category". I just need to print the results of this query with a while loop that will allow me to add options to a select box in HTML
Posted

Try this, watch up for typo, this came straight from my head:
XML
<?php

// create database connection
$mysqli = mysqli_connect('server','username','password','DBName');
// Check connection
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: ".$mysqli->connect_errno);
}

$sql = "SELECT Category FROM Category";

$stmt = $mysqli->prepare($sql);

$stmt->execute();

$resultset = $stmt->get_result()

while ($row = $resultset->fetch_assoc()) {
    $category[$row['category']] = $row['category'];
}

$stmt->close();

$resultset->close();

$mysqli->close();
?>

<select name="category" id="category">

<?php // populate a combo box with $category
        foreach( $category as $key => $value){
?>
<option value="<?php echo $key ?>"><?php echo $value ?></option>
<?php
        }
?>
</select>
 
Share this answer
 
PHP
$con = mysqli_connect(,,,,);
// Check connection
if (mysqli_connect_errno())
   {
   echo "<option>Failed to connect to MySQLi</option>";
   }
 $result = mysqli_query($con,"SELECT * FROM Category");
 while($row = mysqli_fetch_array($result)) {
  echo "<option value="".$row[" category="]."">".$row['Category']."</option>";
  }
 // Free result set
 mysqli_free_result($result);
 mysqli_close($con);
?>

Using what you provided I was able to come up with this, which worked flawlessly. Thanks.
 
Share this answer
 

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