/**
连接数据库
@return resource
/
function connect(){
$mysqli=new mysqli('localhost', 'root', '', 'shopimooc');
if($mysqli->connect_errno>0){
echo "连接错误";
echo $mysqli->connect_error;
exit;
}
$mysqli->query("SET NAMES UTF8");
return $mysqli ;
}
$con=connect();
//得到指定一条记录
/function fetchOne($sql,$result_type=MYSQL_ASSOC){
$result=mysql_query($sql);
$row=mysql_fetch_array($result,$result_type);
return $row;
}
*/
function fetchOne($query,$type="assoc"){
global $con;
$query=mysqli_query($con,$query);
if(!in_array($type,array("assoc","array","row")))
{
die("mysqli_query error");
}
$funcname="mysqlifetch".$type;
return $funcname($query);
}
/function insert($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 insert($table,$array){
global $con;
$keys=join(",",array_keys($array));
$vals="'".join("','",array_values($array))."'";
$sql="insert {$table}($keys) values({$vals})";
mysqli_query($con,$sql);
return mysqli_insert_id($con);
}
/function update($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 update($table,$array,$where=null){
global $con;
foreach($array as $key=>$val){
if($str==null){//报错str未被定义
$sep="";
}else{
$sep=",";
}
$str.=$sep.$key."='".$val."'";
}
$sql="update {$table} set {$str} ".($where==null?null:" where ".$where);
$result=mysqli_query($con,$sql);
//var_dump($result);
//var_dump(mysql_affected_rows());exit;
if($result){
return mysqli_affected_rows($con);
}else{
return false;
}
}
/function delete($table,$where=null){
$where=$where==null?null:" where ".$where;
$sql="delete from {$table} {$where}";
mysql_query($sql);
return mysql_affected_rows();
}/
function delete($table,$where=null){
global $con;
$where=$where==null?null:" where ".$where;
$sql="delete from {$table} {$where}";
mysqli_query($con,$sql);
return mysqli_affected_rows($con);
}
/function fetchAll($sql,$result_type=MYSQL_ASSOC){
$result=mysql_query($sql);
while(@$row=mysql_fetch_array($result,$result_type)){
$rows[]=$row;
}
return $rows;
}/
function fetchAll($sql,$type="assoc"){
global $con;
$query=mysqli_query($con,$sql);
if(!in_array($type,array("assoc",'array',"row"))){
die("mysqli_query error");
}
$funcname="mysqlifetch".$type;
while(@$row=$funcname($query)){
$rows[]=$row;
}
return $rows;
}
/*
function getResultNum($sql){
$result=mysql_query($sql);
return mysql_num_rows($result);
}
/
function getResultNum($sql){
global $con;
$result=mysqli_query($con,$sql);
return mysqli_num_rows($result);
}
/
function getInsertId(){
return mysql_insert_id();
}
*/
function getInsertId(){
global $con;
return mysqli_insert_id($con);
}