时间:2021-07-01 10:21:17 帮助过:14人阅读
<?php $conn=mysql_connect("localhost","root","root"); mysql_query("set names utf8"); mysql_select_db("test"); /** **insert 操作 */ function insert($table,$data){ if(!is_array($data))return false; $sql_key=""; $sql_values=""; foreach($data as $key=>$val){ $sql_key.=$key.","; $sql_values.="'".$val."'".","; } $news_key=trim($sql_key,","); $news_values=trim($sql_values,","); $sql="INSERT INTO `{$table}` ({$news_key}) VALUES ({$news_values})"; $re=query($sql);//执行sql语句 if($re){ return mysql_insert_id();//返回最后一次执行的id号 }else{ return false; } } //查询 function select($tableName,$field="*",$where="",$order="",$limit="",$group="",$having=""){ $sql="select $field from $tableName $where $group $having $order $limit"; $re=query($sql); if(is_resource($re)){ $arr=array(); while($result=mysql_fetch_assoc($re)){ $arr[]=$result; } return $arr; }else{ return false; } } //删除 function delete($tableName,$where=""){ $sql="delete from $tableName $where"; $re=query($sql); if($re){ return mysql_affected_rows (); }else{ return false; } } //修改 function update($tableName,$array,$where=""){ $fvList=""; foreach ($array as $k=>$v) { $fvList.=",".$k."='".$v."'"; } $fvList=substr($fvList,1); $sql="update $tableName set $fvList $where"; echo $sql; $re=query($sql); if($re){ return mysql_affected_rows(); }else{ return false; } } function query($sql){ if(!$sql)return false; return mysql_query($sql); } var_dump(delete('users','where id=1')); ?>
以上就是mysql增删改查--CURD操作的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!