Home php教程 php手册 PHP连接MYSQL数据库通用类

PHP连接MYSQL数据库通用类

Jun 13, 2016 am 10:54 AM
include mysql new php database kind connect Universal

 

include "mysql.php";

$db = new Dirver(); 

$db->DBLink($dbhost='localhost', $dbuser='root', $password='password', $dbname='name', $pconnect=0);

?>

 

 

PHP连接MYSQL数据库类 

mysql.php

 

Class Dirver{

  

 

  //连接数据库

  //link database

  function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){

    $this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");

    //选择连接数据库

    //choose to link database

    @mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);

    return $this->LinkID[$this->Line];

  }

  

  //查询语句

  //view qurry

  function query($query,$limit='') {

    $this -> nums ++;

    //检测如果有限制数据集则处理

    //test if there is finite data,then function

    if($limit>0){

      $query = $query.' LIMIT '.$limit;

    }

    $this-> Lists[$this->Line][] = $query;

    

    

    $querys = mysql_query($query,$this->LinkID[$this->Line]);

    if(!$querys){

      $this->DB_Error($query);

    }

    return $querys;

  }

  

  //返回数组资料

  //back to array info

  function fetch_array($query) {

    return @mysql_fetch_array($query, MYSQL_ASSOC);

  }

  

  //返回数组资料

  //back to array info

  function result($query) {

    return @mysql_result($query,$this->LinkID[$this->Line]);

  }

  

  //返回数组资料行

  //back to row info

  function rows($query) {

    return $this->fetch_array($this->query($query));

  }

  

  

  //返回数组行

  //back to numrows

  function nums($query) {

    return $this->num_rows($this->query($query));

  }

  

  //取得返回列的数目

  //fetch the numbers backing out

  function num_rows($query){

    return @mysql_num_rows($query);

  }

  

  //返回单列的各字段

  //return to every field of single row 

  function fetch_row($query) {

    return @mysql_fetch_row($query);

  }

  

  //返回最后一次使用 INSERT 指令的 ID

  //return to inserted ID used last time

  function insert_id(){

    return @mysql_insert_id($this->LinkID[$this->Line]);

  }

  

  //关闭当前数据库连接

  //close current database link

  function close(){

    return @mysql_close($this->LinkID[$this->Line]);

  }

  

  //检测mysql版本

  //test mysql version

  function version(){

    $query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);

    return  @mysql_result($query, 0);

  }

  

 

  //返回友情提示信息 更多http://www.52mvc.com

  //return to kindly note

  function DB_Error_t($query=''){

  

  }

  function DB_Error($query=''){

    global $PHP_SELF;

      

      //出错语句提示

      //error sentence 

      $errors = preg_replace("/'(.+?)'/is"," '//1' ",mysql_error());

      

 

        $charset ='';

        //提示语言

        //language noted

        $lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',

              'Http Host:', 'Server Name:', 'Server Software:',

              'Host IP Address:', 'Remote User Agent:', 'Current File:', 

              'Current Line:', 'Line.', 'The Error number:', 

              'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');

        //时间处理

        //time

        $nowdate = date('Y-m-d H:i A');

        $errors = preg_replace("/'(.+?)'/is","'//1'",mysql_error());

      

      //检测是否有语句

      //test if there is any sentence.

      if($query==''){

        $query = $lang[14];

      }

      

      echo "

 

 

 

$lang[0]

 

 

 

 

 

         

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

       

            $lang[0]

           

$lang[2]

$nowdate

           

$lang[3]

".$_SERVER['HTTP_HOST']."

           

$lang[4]

".$_SERVER['SERVER_NAME']."

           

$lang[5]

".$_SERVER['SERVER_SOFTWARE']."

           

$lang[6]

".$_SERVER['REMOTE_ADDR']."

           

$lang[7]

".$_SERVER['HTTP_USER_AGENT'].";

           

$lang[11]

".mysql_errno()."

           

$lang[12]

$errors

           

$lang[13]

$query

         

       

      ";

    exit;

  }

 

}

?>

 

PHP连接MYSQL数据库类

include "mysql.php";

$db = new Dirver(); 

$db->DBLink($dbhost='localhost', $dbuser='root', $password='password', $dbname='name', $pconnect=0);

?>

 

 

PHP连接MYSQL数据库类 

mysql.php

 

Class Dirver{

  

 

  //连接数据库

  //link database

  function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){

    $this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");

    //选择连接数据库

    //choose to link database

    @mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);

    return $this->LinkID[$this->Line];

  }

  

  //查询语句

  //view qurry

  function query($query,$limit='') {

    $this -> nums ++;

    //检测如果有限制数据集则处理

    //test if there is finite data,then function

    if($limit>0){

      $query = $query.' LIMIT '.$limit;

    }

    $this-> Lists[$this->Line][] = $query;

    

    

    $querys = mysql_query($query,$this->LinkID[$this->Line]);

    if(!$querys){

      $this->DB_Error($query);

    }

    return $querys;

  }

  

  //返回数组资料

  //back to array info

  function fetch_array($query) {

    return @mysql_fetch_array($query, MYSQL_ASSOC);

  }

  

  //返回数组资料

  //back to array info

  function result($query) {

    return @mysql_result($query,$this->LinkID[$this->Line]);

  }

  

  //返回数组资料行

  //back to row info

  function rows($query) {

    return $this->fetch_array($this->query($query));

  }

  

  

  //返回数组行

  //back to numrows

  function nums($query) {

    return $this->num_rows($this->query($query));

  }

  

  //取得返回列的数目

  //fetch the numbers backing out

  function num_rows($query){

    return @mysql_num_rows($query);

  }

  

  //返回单列的各字段

  //return to every field of single row 

  function fetch_row($query) {

    return @mysql_fetch_row($query);

  }

  

  //返回最后一次使用 INSERT 指令的 ID

  //return to inserted ID used last time

  function insert_id(){

    return @mysql_insert_id($this->LinkID[$this->Line]);

  }

  

  //关闭当前数据库连接

  //close current database link

  function close(){

    return @mysql_close($this->LinkID[$this->Line]);

  }

  

  //检测mysql版本

  //test mysql version

  function version(){

    $query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);

    return  @mysql_result($query, 0);

  }

  

 

  //返回友情提示信息

  //return to kindly note

  function DB_Error_t($query=''){

  

  }

  function DB_Error($query=''){

    global $PHP_SELF;

      

      //出错语句提示

      //error sentence 

      $errors = preg_replace("/'(.+?)'/is"," '//1' ",mysql_error());

      

 

        $charset ='';

        //提示语言

        //language noted

        $lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',

              'Http Host:', 'Server Name:', 'Server Software:',

              'Host IP Address:', 'Remote User Agent:', 'Current File:', 

              'Current Line:', 'Line.', 'The Error number:', 

              'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');

        //时间处理

        //time

        $nowdate = date('Y-m-d H:i A');

        $errors = preg_replace("/'(.+?)'/is","'//1'",mysql_error());

      

      //检测是否有语句

      //test if there is any sentence.

      if($query==''){

        $query = $lang[14];

      }

      

      echo "

 

 

 

$lang[0]

 

 

 

 

 

         

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

         

           

           

         

       

            $lang[0]

           

$lang[2]

$nowdate

           

$lang[3]

".$_SERVER['HTTP_HOST']."

           

$lang[4]

".$_SERVER['SERVER_NAME']."

           

$lang[5]

".$_SERVER['SERVER_SOFTWARE']."

           

$lang[6]

".$_SERVER['REMOTE_ADDR']."

           

$lang[7]

".$_SERVER['HTTP_USER_AGENT'].";

           

$lang[11]

".mysql_errno()."

           

$lang[12]

$errors

           

$lang[13]

$query

         

       

      ";

    exit;

  }

 

}

?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles