Home > php教程 > php手册 > body text

一个常用php mysql数据库连接类

WBOY
Release: 2016-06-13 09:49:19
Original
1025 people have browsed it

这是一很基础的东西我们能过构造函数来实现创建类就自动与mysql服务器进行连接,我们只要设置好 $name,$pass,$table三个变量的值就好了。

 代码如下 复制代码

 class ConnectionMySQL{
     //主机
     private $host="localhost";
     //数据库的username
     private $name="root";
     //数据库的password
     private $pass="";
     //数据库名称
     private $table="phptest";
     //编码形式
     private $ut="utf-8";
 
 
     //构造函数
     function __construct(){
         $this->ut=$ut;
         $this->connect();
 
     }
 
     //数据库的链接
     function connect(){
         $link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
         mysql_select_db($this->table,$link) or die("没该数据库:".$this->table);
         mysql_query("SET NAMES '$this->ut'");
     }
 
     function query($sql, $type = '') {
         if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
         return $query;
     }
 
     function show($message = '', $sql = '') {
         if(!$sql) echo $message;
         else echo $message.'
'.$sql;
     }
 
     function affected_rows() {
         return mysql_affected_rows();
     }
 
     function result($query, $row) {
         return mysql_result($query, $row);
     }
 
     function num_rows($query) {
         return @mysql_num_rows($query);
     }
 
     function num_fields($query) {
         return mysql_num_fields($query);
     }
 
     function free_result($query) {
         return mysql_free_result($query);
     }
 
     function insert_id() {
         return mysql_insert_id();
     }
 
     function fetch_row($query) {
         return mysql_fetch_row($query);
     }
 
     function version() {
         return mysql_get_server_info();
     }
 
     function close() {
         return mysql_close();
     }
 
     //向$table表中插入值
     function fn_insert($table,$name,$value){
         $this->query("insert into $table ($name) value ($value)");
     }
     //根据$id值删除表$table中的一条记录
     function fn_delete($table,$id,$value){
         $this->query("delete from $table where $id=$value");
         echo "id为". $id." 的记录被成功删除!";
     }
 }
 
//调用方法

 $db =  new ConnectionMySQL();
 
 $db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");
 $db->fn_delete('test', 'id', 1);
 
 ?>

这里我要讲述一下关于构造函数

 代码如下 复制代码
 //构造函数
     function __construct(){
         $this->ut=$ut;
         $this->connect();
 
     }


这个页面使用了构造函数特别要主要在函数中不要调用数据库连接类了,否则在当前页面会有多连接连接如果访问过大服务器就会出mysql has gone的提法哦。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!