php连接数据库实例tools

<?php
		//这是一个对MySQL数据库操作的工具类
		include 'config.php';
		class SqlHelper{
				public $host="localhost";
				public $username="root";
				public $password="root";
				public $dbname="lzy123";
				public $conn;
				//析构函数,连接数据库
				public function __construct(){
					$this->conn=mysql_connect($this->host,$this->username,$this->password);
					if(!$this->conn){
						die("连接数据库失败,失败信息".mysql_error());
						}
					mysql_select_db($this->dbname,$this->conn);
					mysql_query("set names utf8");
					}			
				
				//执行dql语句,返回的是资源,需要释放资源
				public function execute_dql($sql){
					$res=mysql_query($sql,$this->conn) or die(mysql_error());
					
					return $res;
				}
				//执行dml语句,返回的是bool值,不需要释放资源
				public function execute_dml($sql){
					$b=mysql_query($sql,$this->conn) or die(mysql_error());;
					if(!$b){
						return 0;
					}
					else{
						//检查是否有改动
						if(mysql_affected_rows($this->conn)>0){
							return 1;//ok
						}
						else {
							return 2;//表示没有行受到影响
						}
					}
				}
				
				//关闭连接的方法
				public function close_connect(){
					if(!empty($this->conn)){
						mysql_close($this->conn);
					}
				}
					
				
			}
?>

通过SqlHelper.class.php文件创建对象,就直接可以连接数据库,调用相应的方法可以执行sql语句


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值