php连接mysql示例
解决方法:
/**
* @param string $con_db_host 主机地址
* @param string $con_db_id 用户名
* @param string $con_db_pass 密码
* @param string $con_db_name 数据库名
* @param string $db_charset 字符编码
*/
public static function mysqlDbConnect($con_db_host,$con_db_id,$con_db_pass, $con_db_name = '', $con_db_port = '3306', $db_charset='utf8') {
$connect = @ new mysqli($con_db_host, $con_db_id, $con_db_pass, $con_db_name, $con_db_port);
if($connect->server_info > '4.1') {
if(!$db_charset){
$db_charset = 'utf8';
}
if($db_charset!='latin1') {
$connect>query("SET character_set_connection=$db_charset, character_set_results=$db_charset, character_set_client=binary");
}
if($connect->server_info > '5.0.1') {
$connect->query("SET sql_mode=''");
}
}
if($con_db_name) {
$connect->select_db($con_db_name);
}
}