I have a drop-down list in HTML, where the contents need to load from my MySQL DB using PHP.
I tried like the following; but no option is loading.
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
$dd_res=mysql_query("Select relation FROM relation_names");
while($r=mysql_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
Also i tried like this:
$q="SELECT DISTINCT *relation FROM relation_names";
$rs=mysql_query($q);
if($rs && mysql_num_rows($rs))
{
while($rd=mysql_fetch_object($rs))
{
echo("<option value='$rd->id'>$rd->name</option>");
}
}
But I don't know why these are not working, kindly help me.
i tried the code below as per comment. but option names are not diplaying.
Use mysqli_query()
insted of mysql_query()
. Because mysql_query()
extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
// Mysql db connection
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$rs = mysqli_query($con,"SELECT DISTINCT relation FROM relation_names");
if($rs && mysqli_num_rows($rs))
{
while($rd=mysqli_fetch_object($rs))
{
echo("<option value='$rd->id'>$rd->name</option>");
}
}
mysqli_close($con);
?>
</select>
Refer below mysqli
extentions
<strong> Select Data </strong>
<select name="data1">
<option value=""> NONE </option>
<?php
// Mysql db connection
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$rs = mysqli_query($con,"SELECT DISTINCT relation FROM relation_names");
if($rs && mysqli_num_rows($rs))
{
while($rd=mysqli_fetch_object($rs))
{
echo("<option value='$rd->id'>$rd->relation</option>");
}
}
mysqli_close($con);
?>
</select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.