prathameshkakade Posted September 1, 2013 Share Posted September 1, 2013 Hello everyone.I was trying to display all the databases available on a server in a dropdown list.But I could not get it.Here is my code. <html> <head> <title> List Databases </title> </head> <body> <?php $connection = mysqli_connect('localhost','root','123456789'); if(!$connection)die(mysqli_connect($connection));else echo "Step 1:<br/>Successfully connected to MySQLi server<br/><hr/>"; $query = mysqli_query($connection,"SHOW DATABASES"); echo "Step 2:<br/>The databases available on this server are :<br/>"; while(($result = mysqli_fetch_assoc($query)) != NULL) { echo $result['Database']."<br/>"; } echo "<hr/>"; echo "<form method = 'POST' action = 'list-db.php'>"; echo "<select name = 'db[]'>"; $a = 0; while($result[$a]!= NULL) { echo "<option>$result[$a]</option>"; $a++; } ?> Please help. Quote Link to comment https://forums.phpfreaks.com/topic/281749-display-all-databases-in-dropdown-menu/ Share on other sites More sharing options...
.josh Posted September 1, 2013 Share Posted September 1, 2013 you already looped through the results once, so first you need to reset the internal pointer: mysqli_data_seek($result,0); Then you need to loop through it same way as before Quote Link to comment https://forums.phpfreaks.com/topic/281749-display-all-databases-in-dropdown-menu/#findComment-1447665 Share on other sites More sharing options...
prathameshkakade Posted September 2, 2013 Author Share Posted September 2, 2013 you already looped through the results once, so first you need to reset the internal pointer: mysqli_data_seek($result,0); Then you need to loop through it same way as before I didn't get you.Can you explain in detail please? Quote Link to comment https://forums.phpfreaks.com/topic/281749-display-all-databases-in-dropdown-menu/#findComment-1447763 Share on other sites More sharing options...
objnoob Posted September 2, 2013 Share Posted September 2, 2013 The result set (records returned), generated by your query "SHOW DATABASES", has already been looped through to display the databases. while($record = mysqli_fetch_assoc($result_set)) { echo $record['Database']."<br />"; } You now need to explicitly set the pointer to the first record (row) of the result set to loop through it again to create the select box. mysqli_data_seek($result_set,0); // set pointer to first record because already at end of records // now can loop through records (result set) again because we set to first record while($record = mysqli_fetch_assoc($result_set)) { echo "<option>{$record['Database']}</option>"; } As Josh pointed out, after fetching all of the records to display the databases, the function mysqli_data_seek() will be used to set the internal pointer of the result set to the first record. This will allow you to loop through the result set again starting from the first record, or any record for that matter specified by mysqli_data_seek() s offset parameter, to create the select box. Quote Link to comment https://forums.phpfreaks.com/topic/281749-display-all-databases-in-dropdown-menu/#findComment-1447784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.