Class Database{
public $dbHost;
public $dbUser;
public $dbPass;
public $dbName;
public $db;
public $conn;
public $sql;
public $query;
public $result = NULL;
function __construct($host, $username, $password, $databaseName){
$this->dbHost = $host;
$this->dbUser = $username;
$this->dbPass = $password;
$this->dbName = $databaseName;
$this->connectDb();
$this->selectDb();
return $this;
}
function connectDb(){
$this->conn = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
return $this;
}
function selectDb(){
$this->db = mysql_select_db($this->dbName);
return $this;
}
function setQuery($sql){
$this->sql = $sql;
return $this;
}
function runQuery(){
$this->query = mysql_query($this->sql, $this->conn) or print_r(mysql_error());
return $this;
}
function countResult(){ // SELECT
return mysql_num_rows($this->query);
}
function affectedRows(){ // INSERT INTO / UPDATE / DELETE FROM
return mysql_affected_rows($this->conn);
}
function getResult(){
if ($this->countResult() > 0){ // IF RESULT IS MORE THAN ZERO
$this->result = array();
while($row = mysql_fetch_assoc($this->query)){
$this->result[] = $row;
}
}
return $this->result; // THIS WILL RETURN NULL OR INTEGER VALUE
}
}
$dbObj = new Database("localhost", "root", "", "dbpeter");
/*
$getResult = $dbObj->setQuery("Select * from tbtemplate")->runQuery()->getResult(); // PHP 5 only continuous
$totalResult= $dbObj->countResult();
*/
$getResult = $dbObj->setQuery("INSERT INTO tbtemplate SET name='" . rand() . "'")->runQuery()->affectedRows(); // PHP 5 only continuous
$getResult = $dbObj->setQuery("Select id, name from tbtemplate")->runQuery()->getResult(); // PHP 5 only continuous
$totalResult= $dbObj->countResult();
if ($totalResult > 0 ){
foreach($getResult as $key=>$value){
echo $value['id'] . '
';
echo $value['name'] . '
';
echo $value['filetype'] . '
';
echo $value['upload_date'] . '
';
echo $value['filesize'] . '
';
}
}else{
//empty data
}
/*
mysql_escape_string
php magic_quote
*/