Example of mysqli database operation class encapsulated by PHP
- 2021-11-24 01:03:51
- OfStack
In this paper, an example is given to describe the mysqli database operation class encapsulated by PHP. Share it for your reference, as follows:
mysqli (MySQL Improvement) is recommended because PHPMySQL operation is abandoned below PHP 5.4. This is an MySQLI database operation class written on the basis of my original MySQL database operation class. The overall code is roughly the same as MySQL database operation class. Instantiate db_class directly
<?php
class db_class{
public $db_url; // Connection address
public $db_username; // Connection name
public $db_userpassword; // Connection password
public $db_name; // Database name
public $db_tablename; // Table name
public $db_conn; // Database connection
public $db_order;
public $db_limit;
public function db_getconn(){ // Connect to a database
$this->db_conn= mysqli_connect($this->db_url,$this->db_username,$this->db_userpassword,$this->db_name);
if (!$this->db_conn)
{
echo " Connect MySQL Failure : " . mysqli_connect_error();
}
}
public function __construct($db_url,$db_username,$db_userpassword,$db_name){ // Constructor assignment
$this->db_url=$db_url;
$this->db_username=$db_username;
$this->db_userpassword=$db_userpassword;
$this->db_name=$db_name;
$this->db_order="";
$this->db_limit="";
$this->db_getconn();
mysqli_query($this->db_conn,'set names utf8');
}
public function db_settablename($db_tablename){ // Set table name
$this->db_tablename=$db_tablename;
}
public function db_setorder($str){ // Sort operation
$this->db_order="order by $str";
}
public function db_setlimit($start,$end){ // Paging operation
$this->db_limit="limit $start,$end";
}
public function db_select($typearr="",$where=""){ // Query operation
if(empty($typearr)){
$typearr="*";
}
else{
$typearr=implode(",",$typearr);
}
if(empty($where)){
$where="";
}else{
$where="where ".$where;
}
$arr=array();
$sql="select $typearr from $this->db_tablename $where $this->db_order $this->db_limit ";
$result = mysqli_query($this->db_conn,$sql);
while($row = $result->fetch_array()) {
$arr[]=$row;
}
return $arr;
}
public function db_update($typearr,$valuearr,$where=""){ // Update operation
$sql="";
if(empty($where)){
$where="";
}else{
$where=" where ".$where;
}
$sql.="update $this->db_tablename set ";
foreach ($typearr as $key=>$value){
if(count($typearr)-1==$key){
$sql.=$value."='".$valuearr[$key]."'";
}else{
$sql.=$value."='".$valuearr[$key]."'".",";
}
}
$sql.=$where;
mysqli_query($this->db_conn,$sql);
}
public function db_delete($typestr,$valuestr){ // Delete operation
$sql="delete from $this->db_tablename WHERE $typestr=$valuestr";
mysqli_query($this->db_conn,$sql);
}
public function db_insert($typearr,$valuearr){ // Insert operation
$sql="insert into $this->db_tablename(".implode(",", $typearr).") values(".implode(",", $valuearr).")";
mysqli_query($this->db_conn,$sql);
}
public function __destruct(){ // The destructor closes the connection
mysqli_close($this->db_conn);
}
}
For more readers interested in PHP related content, please check the topics on this site: "Summary of php+mysqli Database Programming Skills", "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.