概述
奔放的小心
2017-03-08 10:40
已采纳
require_once "../include.php";
//链接数据库
function connect()
{
$link = mysqli_connect(DB_HOST, DB_PWD) or die("数据库打开失败error:" . mysqli_connect_errno());
mysqli_set_charset($link, DB_CHARSET);
mysqli_select_db($link, DB_DBNAME);
return $link;
}
//数据插入
function insert($table,$array){
$link = connect();
$keys =join(",",array_keys($array));
$vals = array_values($array);
$name = '"'.$vals[0].'"';
$key = '"'.$vals[1].'"';
$email = '"'.$vals[2].'"';
$sql = "insert into {$table}($keys) values($name,$key,$email)";
// echo $sql;
mysqli_query($link,$sql);
// echo mysqli_errno($link);
return mysqli_insert_id($link);
}
function insertB($table,$array){
$link = connect();
$keys="`".join("','",array_keys($array))."`";
$vals="'".join("','",array_values($array))."'";
$sql="INSERT INTO `{$table}`($keys) VALUES ({$vals})";
echo $sql;
mysqli_query($link,$sql);
return mysqli_insert_id($link);
}
//记录更新
//update imooc_admin set username='king' where id=1
function update($table, $array, $where=null){
$link = connect();
$str = '';
foreach ($array as $key => $val){
if ($str == null) {
$sep = "";
} else {
$sep = ",";
}
$str.=$sep."`".$key."`"."='".$val."'";
}
$sql = "UPDATE `{$table}` SET {$str}"." ".($where == null? null:"WHERE". " "."`{$table}`".".".$where);
$result = mysqli_query($link,$sql);
if ($result){
return mysqli_affected_rows($link);
} else {
return false;
}
}
//删除记录
function delete ($table,$where){
$link = connect();
$where = $where==null? null:"where"."`".$table."`".".".$where;
$sql = "delete from `{$table}` {$where}";
//echo ($sql);
$reslt = mysqli_query($link,$sql);
if ($reslt){
return mysqli_affected_rows($link);
} else {
return false;
}
}
//得到一条记录
function fetchOne ($sql,$result_type=MYSQLI_ASSOC) {
$link = connect();
$result = mysqli_query($link,$sql);
if (mysqli_errno($link)){
die(mysqli_errno($link));
$mes = mysqli_errno($link);
echo "";
}
$row = mysqli_fetch_array($result);
return $row;
}
//等到结果所有的数据
function fetchAll($sql,$result_type=MYSQLI_ASSOC){
$link = connect();
$result = mysqli_query($link,$sql);
if (mysqli_errno($link)){
die(mysqli_errno($link));
} else {
while ($row = mysqli_fetch_array($result,$result_type)){
$rows [] = $row;
}
return $rows;
}
}
//得到结果的条数
function getResultNum ($sql) {
$link = connect();
$result = mysqli_query($link,$sql);
if ($result) {
return mysqli_num_rows($result);
} else {
return null;
}
}
//得到上一步插入数据的id号码
function getInsertId(){
$link = connect();
return mysqli_insert_id($link);
}
最后
以上就是冷傲水蜜桃为你收集整理的mysql.func.php_大神们,mysql.func.php里的代码怎么改成mysqli,求mysqli函数写的mysql.func.php的代码...的全部内容,希望文章能够帮你解决mysql.func.php_大神们,mysql.func.php里的代码怎么改成mysqli,求mysqli函数写的mysql.func.php的代码...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复