标签:
<?php
$db_host = "localhost:3306";
// database name
$db_name = "hx";
// database username
$db_user = "root";
// database password
$db_pass = "";
$connect = mysql_connect($db_host,$db_user,$db_pass,$db_name);
mysql_set_charset(‘utf8‘);
//var_dump($connect);
if (!$connect)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db($db_name, $connect);
function select($table,$where=‘‘,$order=‘‘,$limits=‘‘,$field=‘‘){
$field=($field==null)?‘*‘:$field;
$where=($where==null)?‘‘:‘ where ‘.$where;
$order=($order==null)?‘‘:‘ order by ‘.$order;
$limits=($limits==null)?‘‘:‘ limit ‘.$limits;
$sele="SELECT ".$field." FROM ".$table.$where.$order.$limits;
return mysql_query($sele);
mysql_close($connect);
}
//插入,二个参数都是必须,后为数组
function insert($table,$columns)
{
foreach($columns as $key=>$values)
{
$addkey.=‘`‘.$key.‘`,‘;
if(is_numeric($values))
{
$addvalues.=$values.‘,‘;
}
else
{
$values=mysql_real_escape_string($values);
$addvalues.=‘\‘‘.$values.‘\‘,‘;
}
}
$addkey=rtrim($addkey,‘,‘);
$addvalues=rtrim($addvalues,‘,‘);
$inse = ‘INSERT INTO ‘.$table.‘(‘.$addkey.‘) VALUES (‘.$addvalues.‘);‘;
return mysql_query($inse);
}
//更新,三个参数都是必须的
function update($table,$data,$where)
{
foreach($data as $k=>$v)
{
if(is_numeric($v))
{
$edit_data.=‘`‘.$k.‘`‘.‘=‘.$v.‘,‘;
}
else
{
$edit_data.=‘`‘.$k.‘`‘.‘=‘."‘".mysql_real_escape_string($v)."‘,";
}
}
$edit_data=rtrim($edit_data,‘,‘);
$upda="UPDATE ".$table." SET ".$edit_data." WHERE ".$where;
return mysql_query($upda);
}
//删除
function delete($table,$where)
{
$dele="DELETE FROM ".$table." WHERE ".$where;
return mysql_query($dele);
}
标签:
原文地址:http://www.cnblogs.com/muxiaoye/p/4329949.html