首页 > 数据库技术 > 详细

SQL 增、删、改、查 封装函数

时间:2016-12-02 22:47:28      阅读:436      评论:0      收藏:0      [点我收藏+]

<?php
//连接数据库
function connects($host,$username,$password,$databaseName){

$conn=mysql_connect($host,$username,$password) or die(‘连接数据库失败‘);

mysql_select_db($databaseName);

mysql_query(‘set names utf8‘);

return $conn;
}
//查询
function Select_tb($tables,$fields=‘*‘,$where=1,$limits=0,$limite=1){

$sql="select {$fields} from {$tables} where {$where} limit $limits,$limite";

$result=mysql_query($sql);

$arr=array();

while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) {
$arr[]=$row;
}

return $arr;
}
//获取一条数据
function Getone($tables,$fields=‘*‘,$where=1){

$sql="select {$fields} from {$tables} where {$where}";

$result = mysql_query($sql) or die(‘执行sql语句出错‘);

return mysql_fetch_array($result);
}
//删除
function deletes($tables,$where){

$sql="delete from {$tables} where {$where}";

$result =mysql_query($sql);

if ($result)
return true;
else
return false;
}
//添加
function Insert($tables,$keys,$values){

$sql="insert into {$tables} ({$keys}) values({$values})";

$result = mysql_query($sql);

if($result)
return true;
else
return false;
}
//修改
function updates($tables,$where=null){

$fields=rtrim($fields,‘,‘); //去掉sql里的最后一个逗号

$where=$where==null?‘‘:‘where‘.$where;

$sql="Update {$tables} set {$fields} {$where}";

$result=mysql_query($sql);

if ($result)
return mysql_affected_rows();
else
return false;
}


?>

SQL 增、删、改、查 封装函数

原文:http://www.cnblogs.com/chung/p/6127313.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!