Index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>
OOP in PHP
</title>
<?php
include("config.php");
include("class_lib.php");
?>
</head>
<body>
<?php
echo "Query time: ".$db->GetDBTime() ."<br />";
echo "Queries Executed: ".$db->getSqlCount() ."<br />";
$stefan = new person();
$stefan->set_name('Candan');
$stefan->set_age(17);
$stefan->set_race('Caucasian');
$stefan->set_birthday('Dec-21-1992');
$stefan->set_email('[email protected]');
$stefan->get_data($stefan->name);
?>
</body>
</html>
db_lib.php
<?php
class DBManager{
var $connection = '';
var $queryCounter = 0;
var $totalTime = 0;
var $errorCode = 0;
var $errorMsg = '';
var $resultSet = '';
function DBConnect($host, $user, $pass, $db) {
$startTime = $this->getMicroTime();
if(!$this->connection = @mysql_connect($host, $user, $pass, true)) {
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
return false;
}
if(!@mysql_select_db($db, $this->connection)) {
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
@mysql_close($this->connection);
return false;
}
$this->totalTime += $this->getMicroTime() - $startTime;
return true;
}
function executeQuery($sql) {
$startTime = $this->getMicroTime();
++$this->queryCounter;
if(!$this->resultSet = @mysql_query($sql, $this->connection)) {
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
$this->totalTime += $this->getMicroTime() - $startTime;
return false;
}
$this->totalTime += $this->getMicroTime() - $startTime;
return $this->resultSet;
}
function getAffectedRows() {
return @mysql_affect_rows($this->connection);
}
function getSelectedRows() {
return @mysql_num_rows($this->resultSet);
}
function getInsertID() {
return @mysql_insert_id($this->connection);
}
function loadResult() {
$array = array();
while($row = mysql_fetch_object($this->resultSet)) {
$array[] = $row;
}
mysql_free_result($this->resultSet);
return $array;
}
function getErrrorCode(){
return $this->errorCode;
}
function getErrorMessage(){
return $this->errorMsg;
}
function getDBTime() {
return round($this->totalTime, 6);
}
function getSqlcount() {
return $this->queryCounter;
}
function getMicroTime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
?>
class_lib.php
<?php
include("db_lib.php");
include("config.php");
$db = new DBManager();
$db->DBConnect($_CONFIG['Host'], $_CONFIG['User'], $_CONFIG['Pass'], $_CONFIG['Name']);
class person {
var $name;
var $age;
var $race;
var $birthday;
var $email;
function set_name($new) {
$this->name = $new;
}
function set_age($new) {
$this->age = $new;
}
function set_race($new) {
$this->age = $new;
}
function set_birthday($new) {
$this->birthday = $new;
}
function set_email($new) {
$this->email = $new;
}
function get_data($name) {
$q = "SELECT * FROM persons WHERE name = '".$name."'";
if(!$res = $db->executeQuery($q)) {
echo "There was an error retrieving data from the database. <br />";
echo "Error code: ".$db->getErrorCode .'<br />';
echo "Error Message: ".$db->getErrorMessage .'<br />';
echo "Query: ".$q;
} else {
if($db->getSelectedRows($res) == 0) {
echo "No data found. <br /> Inserting data, Please be patient.<br />";
insert_data();
echo "Inserted data.";
} else {
echo "<pre>".print_r($res, true)."</pre>";
}
}
}
function insert_data() {
$q = "INSERT INTO persons (name, age, race, birthday, email) VALUES('".$this->name."', ".$this->age.", '".$this->race."', '".$this->birthday."', '".$this->email."')";
if(!$res = $db->executeQuery($q)) {
echo "There was an error insert data into the database. <br />";
echo "Error code: ".$db->getErrorCode .'<br />';
echo "Error Message: ".$db->getErrorMessage .'<br />';
echo "Query: ".$q;
} else {
echo "Query: ".$q."<br />";
echo "Affected Rows: ".$db->getAffectedRows() ."<br />";
}
}
}
?>
En in mijn config staat gewoon de mysql info.
Wat ik krijg op mijn index:
Query time: 0.004515
Queries Executed: 0
Notice: Undefined variable: db in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\oop\class_lib.php on line 51
Fatal error: Call to a member function executeQuery() on a non-object in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\oop\class_lib.php on line 51