Saturday, 12 April 2014

PHP CONNECTIVITY TO SQL

Hi Friedns for doing connectivity to sql database by php here is procedure by using XAMPP software


1) Install XAMPP software in to ur system
2) Save the php files in C:\XAMPP|htdocs folder
3)create database table in SQL
4)select default database test
5) run the file through browser by typing localhost/filname.php



use following code by doing connection
=======================================================================

Filename= 1.php

========================================================================
<?php

error_reporting(0);

$connect=mysql_connect("localhost","root","") or die("Not Connected");

echo"<br>Connecting..........";

mysql_selectdb("test") or die("No database found");

echo"<br>Connected to database";

?>
=========================================================================

Filename=2.php

=========================================================================
html><body>
<form action="2.php" method="post">
<h4>Name</h4><input type="text" name="nm" id="nm">
<h4>Id</h4><input type="text" name="op" id="op">

<select name="query" >
      <option value="1" >Select</option>
      <option value="2" >Update</option>
      <option value="3" >Delete</option>
      <option value="4" >Insert</option>
</select>

<input type="submit" value="Submit Query">


<?php

require("1.php");
$na=$_REQUEST['nm'];
$id1=$_POST['op'];
switch($_POST['query']){

case 1 :
$extract=mysql_query("select * from STUDENT");
 echo "<br>No. of rows in table :".mysql_num_rows($extract)."<table cellspacing='2' border='2'><th>ID</th><TH>Name</th>";
while($row=mysql_fetch_assoc($extract)){
   
    $id=$row['id'];

    $n=$row['name'];
    echo "<tr><td>".$id."</td><td>".$n."</td></tr>";

}
echo "</table>";break;

case 2 :
$update=mysql_query("UPDATE `test`.`STUDENT` SET `name` = '$na' WHERE `STUDENT`.`id` =$id1");
$extract=mysql_query("select * from STUDENT");
 echo "<br>No. of rows in table :".mysql_num_rows($extract)."<table cellspacing='2' border='2'><th>ID</th><TH>Name</th>";
while($row=mysql_fetch_assoc($extract)){
   
    $id=$row['id'];

    $n=$row['name'];
    echo "<tr><td>".$id."</td><td>".$n."</td></tr>";

}
echo "</table>";break;


case 3 :
$del=mysql_query("DELETE FROM `test`.`STUDENT` WHERE `STUDENT`.`id` =$id1");
$extract=mysql_query("select * from STUDENT");
 echo "<br>No. of rows in table :".mysql_num_rows($extract)."<table cellspacing='2' border='2'><th>ID</th><TH>Name</th>";
while($row=mysql_fetch_assoc($extract)){
   
    $id=$row['id'];

    $n=$row['name'];
    echo "<tr><td>".$id."</td><td>".$n."</td></tr>";

}
echo "</table>";break;

case 4 :
$write=mysql_query("Insert into STUDENT values('$id1','$na')");
$extract=mysql_query("select * from STUDENT");
 echo "<br>No. of rows in table :".mysql_num_rows($extract)."<table cellspacing='2' border='2'><th>ID</th><TH>Name</th>";
while($row=mysql_fetch_assoc($extract)){
   
    $id=$row['id'];

    $n=$row['name'];
    echo "<tr><td>".$id."</td><td>".$n."</td></tr>";

}
echo "</table>";break;

}
?>
</body>
</html>
====================================================================

No comments:

Post a Comment