Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/281749-display-all-databases-in-dropdown-menu/
Share on other sites

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.