PHP对数据库操作进行封装

Posted on 2010-01-27 16:05  秋风啸落叶  阅读(321)  评论(0编辑  收藏  举报

 

PHP数据库操作类
 1 <?php
 2 
 3 /*
 4 *        mysql数据库连接、操作类
 5 */
 6 class mysql {
 7   private $link
 8         private $host;
 9         private $name;
10         private $pass;
11         private $table;
12 
13         function __construct($hostID, $nameID, $passID, $tableID) {
14                 $this->host = $hostID;
15                 $this->name = $nameID;
16                 $this->pass = $passID;
17                 $this->table = $tableID;
18                 $this->connect();
19         }
20 
21         function connect() {
22                 $this->link = mysqli_connect($this->host, $this->name, $this->pass) or die(mysqli_error());
23                 $select = mysqli_select_db($this->link,$this->table) or die('没有数据库:' . $this->table);
24                 return $select;
25         }
26 
27         function query($link,$sql) {
28                 $query = mysqli_query($link,$sql) or die("出错啦!");
29                 return $query;
30         }
31 
32         function fn_insert($table, $name, $value) {
33                 $this->query($this->link,"insert into $table ($name) value ($value)");
34         }
35 }
36 $db = new mysql('localhost', 'root', '', 'phpjob');
37 $db->fn_insert('test', 'id,name,value', "'1','jack','usa'");
38 ?>
39