# INTRODUCTION

# MYSQL QUERIES

Connectivity Code for MYSQL DATABASE.

here in given below code localhost is server name and root is username, password is “”(blank).
note: when we upload it on server then we require following:

(1)databse server name.
(2)username.
(3)password.
(4)database name.

<?php

$con=mysql_connect(“localhost”,”root”,””);

mysql_select_db(“needa”,$con);

if($con)
{
echo “connection is successful”;
}
else
{
echo “not connected”;
}
?>

Insert , update , delete ,search in PHP :

First OF ALL create table details :

Create table details

(id int primary key auto_increment ,

name varchar(200),

city varchar(200));

((1) first write code for insert.html :

<form action=”insert.php” method=”post”>

<input type=”text” name=”id”>

<input type=”text” name=”name”>

<input type=”text” name=”city”>

<input name=”myBtn” type=”submit” value=”Submit Data”>

</form>

(2) write code for insert.php :

<?php

$id=$_POST[‘id’];

$nm=$_POST[‘name’];

$cy=$_POST[‘city’];

$con=mysql_connect(‘localhost’,’root’,” );

mysql_select_db(‘needa’,$con);

$sql=”insert into details (id,name,city) values(‘$id’,’$nm’,’$cy’)”;

mysql_query($sql);

echo “record inserted successfully”;

?>

Now write code for update in php:

(1)write code for update.html :

<form action=”update.php” method=”post”>

<input type=”text” name=”id”>

<input type=”text” name=”name”>

<input type=”text” name=”city”>

<input name=”myBtn” type=”submit” value=”Submit Data” >

</form>

(2)write code for update.php file:

<?php

$id=$_POST[‘id’];

$nm=$_POST[‘name’];

$cy=$_POST[‘city’];

$con=mysql_connect(‘localhost’,’root’,” );

mysql_select_db(‘needa’,$con);

$sql=”update details set name =’$nm’, city=’$cy’ where id=’$id'”;

mysql_query($sql);

echo “record updated successfully”;

?>

Now write code delete in php :

(1)write code for delete.html:

<form action=”delete.php” method=”post”>

<input type=”text” name=”id”>

<input name=”myBtn” type=”submit” value=”Submit Data” >

</form>

( (2) write code for “delete.php” file :

<?php

$id=$_POST[‘id’];

$con=mysql_connect(‘localhost’,’root’,” );

mysql_select_db(‘needa’,$con);

$sql=”delete from details where id=’$id'”;

mysql_query($sql);

echo “record deleted successfully”;

?>

now write code for search example :

( (1) write code for search.html :

<form action=”search.php” method=”post”>

<input type=”text” name=”name”>

<input name=”myBtn” type=”submit” value=”Submit Data” >

</form>

(2)write code for search.php :

<?php

$nm=$_POST[‘name’];

$con=mysql_connect(‘localhost’,’root’,” );

mysql_select_db(‘needa’,$con);

$sql=”select * from details where name=’$nm'”;

$result=mysql_query($sql);

while($row=mysql_fetch_array($result))

{

echo $row[‘id’];

echo $row[‘name’];

echo $row[‘city’];

}

?>