Hi,
As i am new in OOP, i just want to ask those who are experts in OOP that can this my example be considered as OOP example?
Thanks
dbprocess.class.php
<?php
class DBprocess {
private $dbconn;
private $db;
public function __construct($host, $user, $username, $password, $database) {
$this->dbconn=mysql_connect($host, $user, $username, $password);
$this->db=mysql_select_db($database);
}
private function executeQuery($sql, $process) {
$runSql=mysql_query($sql, $this->dbconn) or die ("ERROR: Database connection error");
if ($process == "find" || $process == "list") {
if(@mysql_num_rows($runSql)>0) {
while($newArray=mysql_fetch_assoc($runSql)) {
$data[]=$newArray;
}
return $data;
} else {
return "No Record";
}
} else if ($process == "insert" || $process == "delete" || $process == "update") {
if (mysql_affected_rows()>0) {
return "Process is done";
} else {
return "Try again later";
}
}
}
public function findRec($id) {
$sql="SELECT * FROM person WHERE id='$id'";
return $this->executeQuery($sql, "find");
}
public function listRec() {
$sql="SELECT * FROM person";
return $this->executeQuery($sql, "list");
}
public function insertRec($name, $surname) {
$sql="INSERT INTO person (name, surname) VALUES ('$name', '$surname')";
return $this->executeQuery($sql, "insert");
}
public function deleteRec($id) {
$sql="DELETE FROM person WHERE id='$id'";
return $this->executeQuery($sql, "delete");
}
public function updateRec($id, $name, $surname) {
$sql="UPDATE person SET name='$name', surname='$surname' WHERE id='$id'";
return $this->executeQuery($sql, "update");
}
}
?>
index.php
<?php
require_once("dbprocess.class.php");
$result=new DBprocess("localhost", "root", null, null, "classTest");
/* FIND
$data=$result->findRec(4);
echo "<pre>";
print_r($data);
echo "</pre>";
*/
/* LIST
$data=$result->listRec();
echo "<pre>";
print_r($data);
echo "</pre>";
*/
/* INSERT
$data=$result->insertRec("Robert", "De Niro");
print_r($data);
*/
/* DELETE
$data=$result->deleteRec(1);
print_r($data);
*/
/* UPDATE
$data=$result->deleteRec(1, "Ali", "Veli");
print_r($data);
*/
?>