<?php
class MySQL{
var $dbHost="localhost";
var $dbName="mforum";
var $dbTable="users";
var $dbUser="root";
var $dbPassword="";
var $link;
var $status=0;
var $error="";
function MySQL(){//constructor for class MySQL .Have next arguments
//$dbHost,$dbUser,$dbPassword,$dbName
$count_args=func_num_args();
if ($count_args>0){
$host=@func_get_arg(0);
if ($host){
$this->dbHost=$host;
}
$user=@func_get_arg(1);
if ($user){
$this->dbUser=$user;
}
$passwd=@func_get_arg(2);
if ($passwd){
$this->dbPassword=$passwd;
}
$db=@func_get_arg(3);
if ($db){
$this->dbName=$db;
}
}
}
function __destruct(){
if ($this->link)//if($this->link!=false)
mysql_close($this->link);
}
function get_error(){
return $this->error;
}
function set_error($error){
$this->error=$error;
}
function get_link(){
return $this->link;
}
function get_status(){
return $this->status;
}
function get_table(){
return $this->dbTable;
}
function set_table($dbTable){
$this->dbTable=$dbTable;
}
function get_database(){
return $this->dbName;
}
function set_database(){
$count_args=func_num_args();
if ($count_args>0){
$dbName=@func_get_arg(0);
if ($dbName){
$this->dbName=$dbName;
}
}
if ($this->link){
$this->status=@mysql_select_db($this->dbName,$this->link);
if ($this->status==false){
$this->error="error select database ".$this->dbName." ".mysql_error();
}
}
return $this->status;
}
function connect(){
$this->link=mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword)
or $error="error connect to ".$this->dbHost." ".mysql_error();
}
function disconnect(){
$result=false;
if ($this->link){//if($this->link!=false)
mysql_close($this->link);
$result=true;
}
if ($result==false){
$this->error="cant close connection :(";
}
}
function describe_table($dbTable){
$query="describe `".$dbTable."`";
$result=mysql_query($query) or print mysql_error();
$field_list=array();
if ($result!=false){
while ($row=mysql_fetch_row($result)){
$field_list[]=$row[0]; //get field name
}
}
return $field_list;
}
function query($query,$show_query=false){
$result=false;
$result=mysql_query($query) or print mysql_error();
if ($show_query==true){
print '<pre>';
print_r($query);
print '</pre>';
}
return $result;
}
function get_count_fields($dbTable,$where=""){
$query="select count(*) as c from `".$dbTable."` ".$where;
$result=mysql_query($query) or print mysql_error();
$fields_count=false;
if ($result!=false){
$fields_count=mysql_result($result,0,0);
}
return $fields_count;
}
}
?>
|