php常用mysql操作函数

php常用的增删改查的mysql函数,需要先预设一下一些常量,如DB_HOST,DB_USER等,上码!

<?php include_once "config.php"; function connectMysql(){ $link=mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("数据库连接失败:".mysql_errno().":".mysql_error()); mysql_set_charset(DB_CHARSET); mysql_select_db(DB_NAME,$link) or die("数据库打开失败:".mysql_errno().":".mysql_error()); return $link; } function insertMysql($table,$array){ $keys=join(",",array_keys($array)); $vals="'".join("','",array_values($array))."'"; $sql="insert {$table}($keys) values({$vals})"; mysql_query($sql); return mysql_insert_id(); } function updateMysql($table,$array,$where=null){ foreach($array as $key=>$val){ if($str==null){ $sep=""; }else{ $sep=","; } $str.=$sep.$key."='".$val."'"; } $sql="update {$table} set {$str} ".($where==null?null:" where ".$where); $result=mysql_query($sql); //var_dump($result); //var_dump(mysql_affected_rows());exit; if($result){ return mysql_affected_rows(); }else{ return false; } } function deleteMysql($table,$where=null){ $where=$where==null?null:" where ".$where; $sql="delete from {$table} {$where}"; mysql_query($sql); return mysql_affected_rows(); } function fetchoneMysql($sql,$result_type=MYSQL_ASSOC){ $result=mysql_query($sql); $row=mysql_fetch_array($result,$result_type); return $row; } function fetchallMysql($sql,$result_type=MYSQL_ASSOC){ echo "ssd"; $result=mysql_query($sql); while(@$row=mysql_fetch_array($result,$result_type)){ $rows[]=$row; } return $rows; } function getResultNum($sql){ $result=mysql_query($sql); return mysql_num_rows($result); } function getInsertId(){ return mysql_insert_id(); } ?>

你可能感兴趣的:(PHP,mysql,函数)