****
dao.php
<?php
class DAO
{
private static $con;
public function __construct($hostname, $username, $pass, $db)
{
try
{
self::$con = new PDO("mysql:host=$hostname; dbname=$db", $username, $pass);
}
catch (Exception $e)
{
echo 'Error Message';
}
try
{
$x= self:: $con;
}
catch (Exception $e)
{
echo 'Error Message';
}
}
public function insert($stu_info, $data_array)
{
try
{
foreach($data_array as $key=> $value)
{
$querykey[] = $key;
$queryvalue[] = $value;
}
$in = self :: $con -> exec ("insert into $stu_info values ('$queryValue[0]', '$queryValue[1]', '$queryValue[2]', '$queryValue[3]')");
print 'Hello';
}
catch(Exception $e)
{
echo 'Error Message';
}
}
public function fetch ($stu_info, $where_array, $sort_order)
{
foreach($where_array as $roll=> $value)
{
$queryValue[] = $value;
$queryKey[] = $roll;
}
$data = self :: $con -> query("select * from $stu_info where $queryKey[0] = '$queryValue[0]' order by $queryKey[0] $sort_order");
foreach ($data as $row)
{
print $row['id']. ' ' . '<br/>';
print $row['name']. ' ' . '<br/>';
print $row['roll']. ' ' . '<br/>';
print $row['date_birth']. ' ' . '<br/>';
}
}
public function delete ($stu_info, $where_aaray)
{
foreach($where_array as $roll=> $value)
{
$queryValue[] = $value;
$queryKey[] = $roll;
}
$data = self :: $con -> query("DELETE from $stu_info where $queryKey[0] = '$queryValue[0]' order by $queryKey[0]");
print 'Deleted';
}
public function update ($stu_info, $where_aaray)
{
foreach($where_array as $roll=> $value)
{
$queryValue[] = $value;
$queryKey[] = $roll;
}
$in = self :: $con -> exec ("UPDATE stu_info SET ('$queryValue[0]' = 'id', '$queryValue[1]' = 'name', '$queryValue[2]' = 'roll', '$queryValue[3]' = 'date_birth')");
print 'Updated';
foreach ($data as $row)
{
print $row['id']. ' ' . '<br/>';
print $row['name']. ' ' . '<br/>';
print $row['roll']. ' ' . '<br/>';
print $row['date_birth']. ' ' . '<br/>';
}
}
}
?>
**
test.php
**
<?php
require_once('dao.php');
{
$dao = new DAO ("localhost", "ttest", "ttest", "crud2");
if (isset($_POST["s1"]))
{
$dao -> insert ("stu_info", array("id" => NULL, "name" => "Joe", "roll" => "890", "date_birth" => "12-02-1997"));
}
if (isset($_POST["s2"]))
{
$dao -> fetch ("stu_info", array("roll" => "890"), "ASC");
}
if (isset($_POST["s3"]))
{
$dao -> fetch ("stu_info", array("date_birth" => "12-02-1997"));
}
}
?>
<html>
<head>
<title> Test Form </title>
</head>
<body>
<form name="form1" method="post" >
<input type="submit" value="clickToInsert" name="s1" id="s1" /><br/>
<input type="submit" value="clickToFetch" name="s2" id="s2" /><br/>
<input type="submit" value="clickToDelete" name="s3" id="s3" /><br/>
<input type="submit" value="clickToUpdate" name="s4" id="s4" />
</form>
</body>
</html>