code to link database selected through dropdownlist with corresponding database in mysql.Plz reply as soon as possible.
Dropdownlist will list all database names inserted in a particular table

Recommended Answers

All 4 Replies

What have you written so far, where does it go wrong. Please post the problem code so we all can have a look at it.

//php code

<?
mysql_connect("localhost", "root", "123") or die(mysql_error());

mysql_select_db("jai") or die(mysql_error());

$sql="SELECT distinct db_name FROM dbsname";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $db_name=$row["db_name"];
    $options.="<OPTION VALUE=\"$db_name\">".$db_name;
}
?>

<html>
   
<body background="lgrey013.jpg" bgcolor="#FAEBD7">
        <label> Databases Available </label>
      <SELECT NAME=db_name>

          <OPTION VALUE=0>Choose
<?=$options?>
</SELECT>

    </body>

</html>

iam only up to these .i dont have any idea regarding how to choose the selected database name in mysql and list the tables already existing.Plz help me

Did re-write your code. The following should work but you may have to change some things in it.

<?php
// the html header
echo "<html>
<body background=\"lgrey013.jpg\" bgcolor=\"#FAEBD7\">";

// connect with database
mysql_connect("localhost", "root", "123") or die(mysql_error());
mysql_select_db("jai") or die(mysql_error());

// do the query
$sql="SELECT distinct db_name FROM dbsname";
$result=mysql_query($sql);

// create a form with select box
echo "form method=\"post\" action=\"whatever.php\">
<select name=\"db_name\">";

// The while loop is looping through the database rows,
// the for loop is looping through the current database row.
while ($row = mysql_fetch_assoc ($result)) {
	foreach ($row as $var => $value) {
		echo "option value=\"".$var."\">".$value."</option>";
	}
}
echo "</select>
<input type=\"submit\" value=\"submit-form\"/>";

// the html footer
echo "</body>
</html>";
?>

Try like this, it will works for you

<?php
mysql_connect("localhost", "root", "123") or die(mysql_error());
mysql_select_db("jai") or die(mysql_error());

$sql = "SELECT distinct db_name FROM dbsname";
$result = mysql_query($sql);

$options="";

while ($row = mysql_fetch_assoc($result)) {

    $db_name = $row["db_name"];
    $options.="<OPTION VALUE=\"$db_name\">".$db_name."</OPTION><br />";
}
if(isset($_POST['db_name']) && $_POST['db_name']!='')
{
	//perform your other operations based on the selected distinct value
}
?>
<html>
<body background="lgrey013.jpg" bgcolor="#FAEBD7">
<form method="post" name="frmName">
      <SELECT NAME='db_name' onchange="javascript:this.form.submit()">
         <OPTION VALUE=0>Choose</OPTION>
			<?php echo $options; ?>
		</SELECT>
</OPTION>
</body>
</html>
//php code

<?
mysql_connect("localhost", "root", "123") or die(mysql_error());

mysql_select_db("jai") or die(mysql_error());

$sql="SELECT distinct db_name FROM dbsname";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $db_name=$row["db_name"];
    $options.="<OPTION VALUE=\"$db_name\">".$db_name;
}
?>

<html>
   
<body background="lgrey013.jpg" bgcolor="#FAEBD7">
        <label> Databases Available </label>
      <SELECT NAME=db_name>

          <OPTION VALUE=0>Choose
<?=$options?>
</SELECT>

    </body>

</html>

iam only up to these .i dont have any idea regarding how to choose the selected database name in mysql and list the tables already existing.Plz help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.