function connect(){
$link=mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("数据库连接失败Error:".mysql_errno().":".mysql_error());
mysql_set_charset(DB_CHARSET);
mysql_select_db(DB_DBNAME) or die("指定数据库打开失败");
return $link;
}
/**
* 完成记录插入的操作
* @param string $table
* @param array $array
* @return number
*/
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();
}
//update imooc_admin set username='king' where id=1
/**
* 记录的更新操作
* @param string $table
* @param array $array
* @param string $where
* @return number
*/
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;
}
}
/**
* 删除记录
* @param string $table
* @param string $where
* @return number
*/
function delete($table,$where=null){
$where=$where==null?null:" where ".$where;
$sql="delete from {$table} {$where}";
mysql_query($sql);
return mysql_affected_rows();
}
/**
*得到指定一条记录
* @param string $sql
* @param string $result_type
* @return multitype:
*/
function fetchOne($sql,$result_type=MYSQL_ASSOC){
$result=mysql_query($sql);
$row=mysql_fetch_array($result,$result_type);
return $row;
}
/**
* 得到结果集中所有记录 ...
* @param string $sql
* @param string $result_type
* @return multitype:
*/
function fetchAll($sql,$result_type=MYSQL_ASSOC){
$result=mysql_query($sql);
while(@$row=mysql_fetch_array($result,$result_type)){
$rows[]=$row;
}
return $rows;
}
/**
* 得到结果集中的记录条数
* @param unknown_type $sql
* @return number
*/
function getResultNum($sql){
$result=mysql_query($sql);
return mysql_num_rows($result);
}
/**
* 得到上一步插入记录的ID号
* @return number
*/
function getInsertId(){
return mysql_insert_id();
}
2 回答
慕妹0947187
TA贡献1条经验 获得超2个赞
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/1/3 * Time: 15:26 */ require_once '../include.php'; header("Content-Type:text/html;charset=utf-8"); function connect() { $mysqli = new mysqli(DB_HOST,DB_USER,DB_PWD,DB_DBNAME); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); } $mysqli->set_charset(DB_CHARSET); return $mysqli; } function insert($table,$array) { $keys = join(",",array_keys($array)); $vals="'".join("','",array_values($array))."'"; $sql="insert {$table}($keys) values({$vals})"; $link = connect(); mysqli_query($link,$sql); return mysqli_insert_id($link); } 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); $link = connect(); mysqli_query($link,$sql); return mysqli_affected_rows($link); } function delete($table,$where = null) { $where = ($where == null?null:"where".$where); $sql = "delete from {$table}{$where}"; $link = connect(); mysqli_query($link,$sql); return mysqli_affected_rows($link); } function fetchOne($sql,$result_type = MYSQLI_ASSOC) { $link = connect(); $result = mysqli_query($link,$sql); $row = mysqli_fetch_array($result,$result_type); return $row; } function fetchAll($sql,$result_type = MYSQLI_ASSOC) { $link = connect(); $result = mysqli_query($link,$sql); while (@$row = mysqli_fetch_array($result,$result_type)) { $row[] = $row; } return $row; } function getResultNum($sql) { $link = connect(); $result = mysqli_query($link,$sql); return mysqli_num_rows($result); }
添加回答
举报
0/150
提交
取消