Home > Backend Development > PHP Tutorial > General mysql database connection class code_PHP tutorial

General mysql database connection class code_PHP tutorial

WBOY
Release: 2016-07-13 17:04:30
Original
1150 people have browsed it

General mysql tutorial database tutorial connection class code

Database connection is a limited and expensive resource, and database connection affects the performance indicators of the program. The database connection pool was proposed to address this problem. The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of re-establishing one; release database connections whose idle time exceeds the maximum idle time to avoid errors due to failure to release the database connection. Caused by missing database connection. This technology can significantly improve the performance of database operations.


/*
* created on 2010-3-8
* make by:suniteboy
* my first mysql class
*
*/

class mysql{
private $server ="";
private $user ="";
private $pwd ="";
private $database="";
private $linkmode = 1; //Connection mode, 0 means normal connection, 1 means permanent connection
private $conn = 0;
private $sql =""; //sql statement
private $result =""; //query results
private $record =""; //Save record

//============================================
//Constructor
//============================================
public function __construct($server,$user,$pwd,$database,$charset="utf8",$linkmode=0)
{
if(empty ( $server )| empty( $user ) | empty( $database ))
{
$this->show_error("The connection information is incomplete, please check whether the server address, user name and connected database information are provided");
Return 0;
}
$this->server = $server;
$this->user = $user;
$this->pwd = $pwd;
$this->database = $database;
$this->charset = $charset;
$this->linkmode = $linkmode;
$this->connect();
}

//============================================
//Connection function
//============================================
public function connect()
{
$this->conn = $this->linkmode?mysql_pconnect($this->server,$this->user,$this->pwd):
mysql_connect($this->server,$this->user,$this->pwd);
if(!$this->conn)
{
$this->show_error('Unable to connect to server');
Return 0;
}

if(!mysql_select_db($this->database))
{
$this->show_error('Unable to connect to database'.$this->database);
Return 0;
}
// $this->query('set names '.$this->charset);
return $this->conn;

}
//============================================
// mysql query function
//============================================
public function query($sql)
{
if(empty($sql))
{
$this->show_error('sql statement is empty');
Return 0;
}
$this->sql = preg_replace('/ {2,}/',' ',trim($sql));
$this->result = mysql_query($this->sql,$this->conn);
if(!$this->result)
{
$this->show_error('sql statement error',true);
Return 0;
}
return $this->result;
}

//============================================
// Function
//============================================
public function select_db($dbname)
{
Return mysql_select_db($dbname);
}

public function fetch_array($query,$result_type=mysql_assoc)
{
Return mysql_fetch_array($query,$result_type);
}

public function fetch_row($query)
{
Return mysql_fetch_row($query);
}
//============================================
// Get the number of record rows affected by the previous mysql operation
//============================================
public function affected_rows()
{
Return mysql_affected_rows();
}
public function num_fields($query)
{
Return mysql_num_fields($query);
}

public function num_rows($query)
{
Return @mysql_num_rows($query);
}

public function insert_id()
{
Return mysql_insert_id();
}

public function close()
{
Return mysql_close();
}
//============================================
// Get a result from the record
//============================================
public function getone($sql)
{
$res = $this->query($sql);
if($res!==false)
{
$row = mysql_fetch_row($res);
if($row!==false)
{
Return $row;
}
else
{
Return '';
}
}
else
{
Return false;
}
}

//============================================
// Get all results from the record
//============================================
public function getall($sql)
{
$res = $this->query($sql);
if($res!==false)
{
$arr = array();
while($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
Return $arr;
}
else
{
Return false;
}
}

//============================================
// Error prompt function
//============================================
public function show_error($msg='',$sql=false)
{
$err = '['.mysql_errno().']'.mysql_error();
if($sql) $sql='sql statement:'.$this->sql;
if($msg=='')
{
echo $err;
echo "
";
}
elseif($sql &&$msg)
{
echo $msg;
echo "
";
echo $sql;
}
else
{
echo $msg;
echo "
";
}

}

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630850.htmlTechArticleGeneral mysql tutorial database tutorial connection class code Database connection is a limited and expensive resource, and database connection affects the program performance indicators. Database connection pool is exactly for this...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template