Wednesday, November 21, 2018

How can fetch the records in MySQL using php?

Create  a  form.php  for the inserting data & view.php for the fetching data in the  Data base

  • Fetching Records  in the database using Mysql_fetch_array ( ) Function.
  • Select Command use for the fetching data in database.
       make a form.php for inserting data & below view.php for fetch the records

<?php
mysql_connect("localhost","root","");
mysql_select_db("project");

//insert data in database using submit button request
if (isset($_REQUEST['submit']))
{
$n=$_REQUEST['name'];
$p=$_REQUEST['pass'];
$g=$_REQUEST['gender'];
$h=$_REQUEST['chk'];
if ($h)
{
$ch=implode(",",$h);//implode is a string function that are implode a multipal array value like chk[];
}
$ins="insert into user(name,pass,gender,hobby) values('$n','$p','$g','$ch')";
$ex=mysql_query($ins);
header ("location:view.php");//redirect the page on view.php
}
?>
<html>
<head>
<title>insert the data</title>
</head>
<body>
<form method="post">
    <table border="1" align="center">
     <tr>
         <td>name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
         <td>password</td>
            <td><input type="text" name="pass"></td>
        </tr>
        <tr>
         <td>male</td>
            <td><input type="radio" name="gender" value="male">male<input type="radio" name="gender" value="female">female</td>
        </tr>
        <tr>
         <td>hobby</td>
            <td><input type="checkbox" name="chk[]" value="cricket">cricket<input type="checkbox" name="chk[]" value="hockey">hockey</td>
        </tr>
        <tr>
         <td colspan="2"><input type="submit" name="submit" value="submit"></td>
        </tr>
    </table>
    </form>
</body>
</html>

View.php for fetching data

<?php
mysql_connect("localhost","root","");
mysql_select_db("project");

$s="select * from user";//select all records from user table
$ex=mysql_query($s);


?>
<html>
<head>
<style type="text/css">
table th{ background:#F93; color:white;}
table td{ background:#FC9;}
</style>
</head>
<body>
<form method="post">
<table  align="center">
<tr>
<th>id</th>
    <th>name</th>
    <th>pass</th>
    <th>gender</th>
    <th>hobby</th>
    
</tr>
<?php
while ($ft=mysql_fetch_array($ex))   / /fetch the records using  mysql_fetch_array with  while loop
{
?>
<tr>
<td><?php echo $ft['id']; ?></td>
    <td><?php echo $ft['name']; ?></td>
    <td><?php echo $ft['pass']; ?></td>
    <td><?php echo $ft['gender']; ?></td>
    <td><?php echo $ft['hobby']; ?></td>
</tr>
<?php
}
?>

</table>
</form>
</body>
</html>

Output on a Browser:- 

0 comments:

Post a Comment