SQL 增删改查 封装函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 增删改查 封装函数相关的知识,希望对你有一定的参考价值。

<?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 增删改查 封装函数的主要内容,如果未能解决你的问题,请参考以下文章

Python编程 模拟SQL语句 实现对员工信息的增删改查

SQl常用增删改查

类(面向对象增删改查继承多态封装反射)

SQL的增删改查

增删改查sql语句

sql增删改查的操作