select option in php using database

I will show you how to add data into select option by fetching data from database. its quite simple. select option is mostly used for registrations & other major activities & forms. select option is useful.

create database named premal. add 1 table named location_country with 2 fields. 1 is for id autoincrement & name.

premal katigar

After that some data into database.

Now get following Code.

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("premal",$con);
?>
<select name="location" id="location">
$query="select name from location_country";
 $result=mysql_query($query);
 if($result)
 {
 ?>
 <option value="NULL">SELECT</option>
 <?php
 while($row=mysql_fetch_array($result))
 {
 ?>
 <option value="<?php $row[0] ?>"><?php echo $row[0] ?></option>
 <?php
 }
 }
 ?> 


</select>
Advertisements

how to connect database in php

It’s quite Simple to connect to the database of php that is MY_SQL.

Simple Create a new php file with database.php.

database

$p=mysql_connect(“localhost”,”root”)

localhost is host name & root is user name.

mysql_select_db(“psk”,$p);

Above sentence is for selecting database my database is psk & we have to pass one variable that is of connection. I have pass $p.

mysql_query() is used for execute query. I have used for update query.

 

Congo. You got connect with database.