//使用方法:$sql = '';  $data = db_switch('you db','select * from tb','2');
function db_switch($db,$sql,$type='2',$utf='utf8'){
    if (empty($sql))
        die('sql is empty');
    switch ($db) {
        case 'your database name':
            return db_connect(ip,user,passwd,db,$sql,$type,$utf);
            break;

        default:
            echo 'please enter you db_name,like bm、shop、live。';
    }
}

function db_connect($ip,$root,$pass,$db,$sql,$type,$utf){
    $db = @mysqli_connect($ip,$root,$pass,$db) or die("Error " . mysqli_error($link));
    $db->set_charset($utf);
    $data = $db->query($sql);
    $res = array();
    if ($type=='1'){//查询一条数据
        $res = $data->fetch_assoc();
    }elseif($type=='2'){//查询多条
        $res = $data->fetch_all(MYSQLI_ASSOC);
    }elseif ($type=='3'){//更新和删除
        $res = $db->affected_rows;
    }
    $db->close();
    return $res;
}

标签: mysql

仅有一条评论

  1. 不错不错,我喜欢看 https://www.jiwenlaw.com/

添加新评论