php对数据库增删改查操作类

时间:2015-01-05 10:46:38   收藏:0   阅读:178
 1 <?php 
 2     
 3     /**
 4     * 函数名称:SqlTool.class.php 
 5     * 函数功能:php对数据库增删改查操作类
 6     * 函数作者:张真贵
 7     * 创建时间:2015-01-05
 8     * 修改时间:
 9     */
10     header("Content-Type:text/html;charset=utf-8");
11     class SqlTool{
12         private $conn;
13         private $host = ‘localhost‘;
14         private $root = ‘root‘;
15         private $password = ‘‘;
16         private $dbname = test;
17 
18         function __construct(){
19             # code...
20             $this->conn = mysql_connect($this->host,$this->root,$this->password) or die(‘连接数据库失败‘.mysql_error());
21             mysql_select_db($this->dbname);
22             mysql_set_charset(‘utf8‘);
23         }
24 
25         /***************************dql操作***********************************/
26         public function execute_dql($sql){
27             $res = mysql_query($sql,$this->conn) or die(mysql_error());
28             return $res;
29         }
30 
31         /****************************dml操作***********************************/
32         public function execute_dml($sql){
33             $res = mysql_query($sql,$this->conn) or die(mysql_error());
34             if (!$res) {
35                 # code...
36                 return 0;
37             }elseif (mysql_affected_rows($this->conn) > 0) {
38                 # code...
39                 return 1;
40             }else{
41                 # code...
42                 return 2;
43             }
44         }
45     }
46 
47 /*******************
48     $sql = "insert into user1(id,name) values(‘7‘,‘陆逊‘)";
49     //创建对象
50     $sqlTool = new SqlTool;
51     $result = $sqlTool-> execute_dml($sql);
52     if ($result == 0) {
53         # code...
54         echo "失败";
55     }elseif ($result == 1) {
56         # code...
57         echo "成功";
58     }elseif ($result == 2) {
59         # code...
60         echo "没有影响行数";
61     }
62 *******************/    
63  ?>

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!