Can you not just run a query to get the values, echo the HTML out to create the drop-down, and that’s the job done? Or, like the other poster, do you want to populate a second drop-down based on the value selected in a first drop-down? Post the code you have tried with a bit of an explanation as to where it needs to sit.
<?php
$conn = new mysqli('localhost', 'root', 'root', 'crud_tutorial')
or die ('Cannot connect to db');
$result = $conn->query("select name from customers order by name asc");
echo "<select name='name'>";
echo '<option>Select Name</option>';
while ($row = $result->fetch_assoc()) {
unset( $name);
$name = $row['name'];
echo '<option value="'.$name.'">'.$name.'</option>';
}
echo "</select>";
?>
It shows the name. But I do not want to write database connection again. Thanks
Oh I see, the question isn’t about the drop-down, it’s about how to use the database connection based on that database class? Isn’t that the same as you were doing in your other thread? I think the key lines in index.php on that page are 25-26, where they include the database class and create the database object.
As you see in each of their sample codes, every new PHP script will either include
or require
the database class script before it is used, and then create a connection to it.
Example please,
As I said above, if you read the article you pointed to, lines 25 and 26 of their index.php
show how to include the database class file, and create the object so it can be used.
How can i include ‘database.php’; more time in the same page?
Why do you need to include it again in the same page? Once you’ve included it, and created the object (which I think was the second of those two lines I linked to), then you just use the same object for your connections. How is your code laid out that you would need to include it more than once?
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.