//连接数据库
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;
}
?>
本文档介绍了如何通过PHP连接MySQL数据库,包括函数`connects()`用于连接、`Select_tb()`执行SQL查询、`Getone()`获取单条数据、`deletes()`删除记录、`Insert()`插入数据和`updates()`更新数据。还展示了如何使用参数化查询进行高效和安全的操作。
247

被折叠的 条评论
为什么被折叠?



