当前位置:Gxlcms > 数据库问题 > SQL 增、删、改、查 封装函数

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

时间:2021-07-01 10:21:17 帮助过:31人阅读

<?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 增、删、改、查 封装函数

标签:upd   数据库   失败   set   one   封装   res   connect   逗号   

人气教程排行