****

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>

Recommended Answers

All 13 Replies

And what is the error exactly?

hlo dear,
the error is

Fatal error: Call to a member function exec() on a non-object in C:\wamp\www\new DAO\dao.php on line 37

Member Avatar for diafol
$queryvalue[] = $value;

and

"insert into $stu_info values ('$queryValue[0]', '$queryValue[1]', '$queryValue[2]', '$queryValue[3]')"

Several things...

1) Case mismatch: $queryvalue and $queryValue - variable names are case-sensitive
2) Array variables need to be "braced out" in strings:

"INSERT INTO $stu_info VALUES ('{$queryvalue[0]}', '{$queryvalue[1]}', '{$queryvalue[2]}', '{$queryvalue[3]}')"

hlo,
i am getting ur point about case sensitive... i had improve it.... but it can't working till... :-(

again the error is
Fatal error: Call to a member function exec() on a non-object in C:\wamp\www\new DAO\dao.php on line 37

Member Avatar for diafol

Try placing some hard-coded values in the SQL to see if that works...

"INSERT INTO $stu_info VALUES ('One', 'Two', 'Three', 'Four')"

Alternatively, you could use bound parameters - either ? notation or named notation...

$in = self::$con->prepare("insert into $stu_info values (?,?,?,?)");
$in->execute($queryvalue);    

not working

plz help me..... m very upset bcoz of this query

Member Avatar for diafol

You've got to get out more.

help me...... what i can do???

Member Avatar for diafol

so what's happening now? Any error? Same error? If you don't provide the information required for people to help you, then you'll find that people... well, won't help you.

Help yourself by posting all the relevant info.

same error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.