Home  >  Article  >  Backend Development  >  php mysql database operation class_PHP tutorial

php mysql database operation class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:14730browse

Copy code The code is as follows:

/*
 *    mysql数据库 DB类
 *    @package    db
 *    @author        yytcpt(无影)
 *    @version    2008-03-27
 *    @copyrigth    http://www.d5s.cn/ 
 */
class db {
    var $connection_id = "";
    var $pconnect = 0;
    var $shutdown_queries = array();
    var $queries = array();
    var $query_id = "";
    var $query_count = 0;
    var $record_row = array();
    var $failed = 0;
    var $halt = "";
    var $query_log = array();
    function connect($db_config){
        if ($this->pconnect){
            $this->connection_id = mysql_pconnect($db_config["hostname"], $db_config["username"], $db_config["password"]);
        }else{
            $this->connection_id = mysql_connect($db_config["hostname"], $db_config["username"], $db_config["password"]);
        }
        if ( ! $this->connection_id ){
            $this->halt("Can not connect MySQL Server");
        }
        if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){
            $this->halt("Can not connect MySQL Database");
        }
        if ($db_config["charset"]) {
            @mysql_unbuffered_query("SET NAMES '".$db_config["charset"]."'");
        }
        return true;
    }
    //发送SQL 查询,并返回结果集
    function query($query_id, $query_type='mysql_query'){
        $this->query_id = $query_type($query_id, $this->connection_id);
        $this->queries[] = $query_id;
        if (! $this->query_id ) {
            $this->halt("查询失败:n$query_id");
        }
        $this->query_count++;
        $this->query_log[] = $str;
        return $this->query_id;
    }
    //发送SQL 查询,并不获取和缓存结果的行
    function query_unbuffered($sql=""){
        return $this->query($sql, 'mysql_unbuffered_query');
    }
    //从结果集中取得一行作为关联数组
    function fetch_array($sql = ""){
        if ($sql == "") $sql = $this->query_id;
        $this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC);
        return $this->record_row;
    }
    function shutdown_query($query_id = ""){
        $this->shutdown_queries[] = $query_id;
    }
//Get the number of rows in the result set, only for INSERT, UPDATE or DELETE
function affected_rows() {
return @mysql_affected_rows($this->connection_id);
}
/ / Get the number of rows in the result set, only valid for SELECT statements
function num_rows($query_id="") {
if ($query_id == "") $query_id = $this->query_id;
Return @mysql_num_rows($query_id);
}
//Return the numeric code of the error message in the previous MySQL operation
function get_errno(){
$this->errno = @mysql_err no ($this->connection_id);
return $this->errno;
}
//Get the ID generated by the previous INSERT operation
function insert_id(){
return @ mysql_insert_id($this->connection_id);
}
//Get the number of queries
function query_count() {
return $this->query_count;
}
// Release results memory Function Free_Result ($ query_id = "") {
if ($ query_id == "") $ query_id = $ This- & gt; query_id; sult ($ query_id);

// Close mysql connection
Function Close_db () {
IF ($ this- & gt; connection_id) Return @Mysql_Close ($ this- & gt; connection_id);
} >/ / List the tables in the MySQL database
function get_table_names(){
global $db_config;
$result = mysql_list_tables($db_config["database"]);
$num_tables = @my sql_numrows($ result);
for ($i = 0; $i < $num_tables; $i++) {
$tables[] = mysql_tablename($result, $i);
}
my sql_free_result( $result);
return $tables;
if ($query_id == "") $query_id = $this->query_id;
while ($field = mysql_fetch_field($query_id)) {
$fields[] = $field;
}
return $fields;
}
    //错误提示
    function halt($the_error=""){
        $message = $the_error."
rn";
        $message.= $this->get_errno() . "
rn";
        $sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")";
        @mysql_unbuffered_query($sql);
        if (DEBUG==true){
            echo "MySQL 数据库错误";
            echo "rn";
            echo "rn";
            echo "
rn";
            echo "" . htmlspecialchars($message) . "rn";
            echo "
rn";
            exit;
        }
    }
    function __destruct(){
        $this->shutdown_queries = array();
        $this->close_db();
    }
    function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
        $sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:"");
        return $sql;
    }
    function sql_insert($tbname, $row){
        foreach ($row as $key=>$value) {
            $sqlfield .= $key.",";
            $sqlvalue .= "'".$value."',";
        }
        return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")";
    }
    function sql_update($tbname, $row, $where){
        foreach ($row as $key=>$value) {
            $sqlud .= $key."= '".$value."',";
        }
        return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where;
    }
    function sql_delete($tbname, $where){
        return "DELETE FROM `".$tbname."` WHERE ".$where;
    }
    //新增加一条记录
    function row_insert($tbname, $row){
        $sql = $this->sql_insert($tbname, $row);
        return $this->query_unbuffered($sql);
    }
//Update the specified record
function row_update($tbname, $row, $where){
$sql = $this->sql_update($tbname, $row, $where);
return $this->query_unbuffered($sql);
}
//Delete records that meet the conditions
function row_delete($tbname, $where){
$sql = $this-> sql_delete($tbname, $where); where query conditions, $limit returns records, $fields returns fields
*/
function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id ", $sort="DESC"){
$sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort);
return $this-> ;row_query($sql);
}
//Display a record in detail
function row_select_one($tbname, $where, $fields="*", $orderby="id"){
$sql = $this->sql_select($tbname, $where, 1, $fields, $orderby);
return $this->row_query_one($sql);
}
function row_query( $sql){
$rs = $this->query($sql);
$rs_num = $this->num_rows($rs);
$rows = array();
for($i=0; $i<$rs_num; $i++){
$rows[] = $this->fetch_array($rs);
}
$this->free_result ($rs);
return $rows;
}
function row_query_one($sql){
$rs = $this->query($sql);
$row = $ this->fetch_array($rs);
$this->free_result($rs);
return $row;
}
//Count statistics
function row_count($tbname , $where=""){
$sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:"");
         $row = $this->row_query_one($sql);
                                                                                                                                                              $row                                                                      $row It’s been a long time since I’ve posted anything, so I’m sharing some of my commonly used PHP files.
If you add new features or make improvements, please share them with everyone.



Copy code

The code is as follows:

$db_config["hostname"] = "127.0.0.1"; //Server address
$db_config["username"] = "root"; //Database user Name
$db_config["password"] = "root"; = "utf8";
include('db.php');
$db = new db();
$db->connect($db_config);
//Example: query table All records with cid=1 in table_name.
$row = $db->row_select('table_name', 'cid=1');
?>


For more detailed usage, please refer to the db class file comments in .


http://www.bkjia.com/PHPjc/318985.html

truehttp: //www.bkjia.com/PHPjc/318985.htmlTechArticleCopy the code as follows: ?php /* *mysql database DB class*@packagedb *@authoryytcpt(无影) *@version2008-03-27 *@copyrigthhttp://www.d5s.cn/ */ classdb{ var$connection_id=""; var$p...
Statement:
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