PHP MySql的dql和dml操作的封装,成为固定的工具类
2015-07-14 20:18
1001 查看
mysql_class.php:
<span style="font-size:18px;"><?php
class SqlTool{
private $conn;
private $host="localhost";
private $user="root";
private $password="111111";
private $db="test";
function SqlTool(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("对不起,连接数据库失败<br/>错误原因:".mysql_error());
}
mysql_select_db($this->db,$this->conn);//选择数据库
mysql_query("set names utf8");
}
//实现对 dql语句:select 的封装
public function execute_dql($sql){
//执行数据库dql语句,即查询操作
$res=mysql_query($sql,$this->conn) or die("查询失败,失败原因".mysql_error());
return $res;
}
//实现对 dml语句:update,delete ,insert 的封装
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
//return 0; //运行失败
echo "对不起,操作失败";
}else{
//受影响,返回行数;不受影响,返回-1
if(mysql_affected_rows($this->conn)>0){
//return 1; //运行成功
echo "操作成功!";
}else{
//return 2; //成功,但没有影响行数
echo "操作成功,但是行数没有受到影响";
}
}
//关闭连接
mysql_close($this->conn);
}
}
?>
</span>
调用上面文件生成的类,实现数据库的增删改查操作:
<span style="font-size:18px;"><?php
class SqlTool{
private $conn;
private $host="localhost";
private $user="root";
private $password="111111";
private $db="test";
function SqlTool(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("对不起,连接数据库失败<br/>错误原因:".mysql_error());
}
mysql_select_db($this->db,$this->conn);//选择数据库
mysql_query("set names utf8");
}
//实现对 dql语句:select 的封装
public function execute_dql($sql){
//执行数据库dql语句,即查询操作
$res=mysql_query($sql,$this->conn) or die("查询失败,失败原因".mysql_error());
return $res;
}
//实现对 dml语句:update,delete ,insert 的封装
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
//return 0; //运行失败
echo "对不起,操作失败";
}else{
//受影响,返回行数;不受影响,返回-1
if(mysql_affected_rows($this->conn)>0){
//return 1; //运行成功
echo "操作成功!";
}else{
//return 2; //成功,但没有影响行数
echo "操作成功,但是行数没有受到影响";
}
}
//关闭连接
mysql_close($this->conn);
}
}
?>
</span>
调用上面文件生成的类,实现数据库的增删改查操作:
<span style="font-size:18px;"><?php require_once "mysql_class.php";//引入SqlTool的类文件 /************************dml操作******************************/ /*//添加数据 $sql="insert into test (name,id,class,age) values('xuning_a',md5('78989'),'xiaohei@qq.com',18)";//Sql语句 $SqlTool=new SqlTool(); $res=$SqlTool->execute_dml($sql); */ //删除数据 /* $sql="delete from test1 where id = 21";//Sql语句 $SqlTool=new SqlTool(); $res=$SqlTool->execute_dml($sql); */ //修改数据 //$sql="update test set age=1000 where id=2";//Sql语句 //$SqlTool=new SqlTool(); //$res=$SqlTool->execute_dml($sql); //dml操作,不需要再释放资源,只要最后关闭链接即可 /**********************dql操作*********************************/ //查询操作 $sql="select * from test"; $SqlTool=new SqlTool(); $res=$SqlTool->execute_dql($sql); while($row=mysql_fetch_row($res)){ //显示所有的用户数据 foreach($row as $key =>$val){ echo "$key=>"."$val"." "; } echo "<br/>"; } //dql操作要释放资源,并关闭链接 mysql_free_result($res); ?> </span>
相关文章推荐
- 三类,23种设计模式,速记理解法!PHP
- thinkphp3.2.3中设置路由,优化url
- php开发过程中用什么方法来加快页面的加载速度
- PhpStrom常用快捷键
- php获取指定日期所在星期的开始时间与结束时间
- 实习总结(1)
- PHP开发工作心得
- php 手机号判断问题
- layoutParams-动态更改某个控件的margin
- 解决PHP Warning: Cannot modify header information - headers already sent by...
- phpcms 相关阅读 热点阅读 文章上一页 下一页
- PHP常用函数
- php 常用header
- php中form提交默认是什么方式
- PHP IDE 框架 服务器 相关
- 转 PHP常用正则表达式汇总
- ajax + php + Controller 控制所有后台函数调用
- php 流
- js、php、html的几种跳转方式
- PHP函数补完:stream_context_create()模拟POST/GET