php calls the mysql data dbclass class

  • 2020-05-05 11:01:52
  • OfStack

 
class dbClass{ // Start database class  
var $username; 
var $password; 
var $database; 
var $hostname; 
var $link; 
var $result; 

function dbClass($username,$password,$database,$hostname="localhost"){ 
$this->username=$username; 
$this->password=$password; 
$this->database=$database; 
$this->hostname=$hostname; 
} 

function connect(){ // This function is used to connect to a database  
if(!$this->link=mysql_connect($this->hostname,$this->username,$this->password)) 
$this->halt("Sorry,can not connect to database"); 

if($this->version() > '4.1') { 
global $dbcharset,$charset; 
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) { 
$dbcharset = str_replace('-', '', $charset); 
} 
if($dbcharset) { 
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary"); 
} 
} 
if($this->version() > '5.0.1') { 
mysql_query("SET sql_mode=''"); 
} 

return $this->link; 
} 

function select(){ // This function is used to select the database  
mysql_select_db($this->database,$this->link); 
} 

function query($sql){ // This function is used to send out a query and return the result.  
if($this->result=mysql_query($sql,$this->link)) return $this->result; 
else { 
$this->halt("SQL Statement error:  <font color=red>$sql</font><br><br> Error message:  ".mysql_error()); 
return false; 
} 
} 

/* 
 The following functions are used to fetch an array from the result, generally with  while() Circulation, $db->query($sql)  Used in conjunction with, for example:  
$result=query("select * from mytable"); 
while($row=$db->getarray($result)){ 
echo "$row[id] "; 
} 
*/ 
function getarray($result){ 
return @mysql_fetch_array($result); 
} 

/* 
 � �   The following function is used to obtain SQL The first row of the query, which is generally used to check whether a qualified row exists, for example:  
 The user name that the user submitted from the form $username , password, $password Whether in the user table" user ", and returns its corresponding array:  
if($user=$db->getfirst("select * from user where username='$username' and password='$password' ")) 
echo " welcome  $username  , your ID is  $user[id]  . "; 
else 
echo " Wrong username or password! "; 
*/ 
function getfirst($sql){ 
return @mysql_fetch_array($this->query($sql)); 
} 

/* 
 � �   The following functions return the total number of rows that meet the query criteria, such as for pagination calculation, etc., for example:  
$totlerows=$db->getcount("select * from mytable"); 
echo " A total of  $totlerows  Pieces of information. "; 
*/ 
function getcount($sql){ 
return @mysql_num_rows($this->query($sql)); 
} 

/* 
 � �   The following functions are used to update the database, such as a user changing a password:  
$db->update("update user set password='$new_password' where userid='$userid' "); 
*/ 
function update($sql){ 
return $this->query($sql); 
} 

/* 
 � �   The following functions are used to insert a row into the database, such as adding a user:  
$db->insert("insert into user (userid,username,password) values (null,'$username','$password')"); 
*/ 
function insert($sql){ 
return $this->query($sql); 
} 

function getid(){ // This function is used to get the newly inserted row id 
return mysql_insert_id(); 
} 

function num_rows($query) { 
$query = mysql_num_rows($query); 
return $query; 
} 

function num_fields($query) { 
return mysql_num_fields($query); 
} 

function free_result($query) { 
return mysql_free_result($query); 
} 

function version() { 
return mysql_get_server_info(); 
} 

function close() { 
return mysql_close(); 
} 

function halt($message = '') { 
return $message; 
} 
} 

$db=new dbClass("$db_username","$db_password","$db_database","$db_hostname"); 
$db->connect(); 
$db->select(); 


Related articles: