<?php //MySQL连接数据库 function db_connmysql($dbpassword,$dbdatabase,$dbhost='localhost',$dbname='root',$dbcharset='UTF8'){ $link = mysql_connect($dbhost, $dbname, $dbpassword) or die(mysql_error()); $dbVersion = mysql_get_server_info($link); //设置字符集 mysql_set_charset($dbcharset,$link); //mysql_query("SET NAMES gb2312", $link);此方式不推荐; //设置 sql_model if($dbVersion >'5.0.1'){ mysql_query("SET sql_mode=''",$link); mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $link);// 防止宽字节注入 } //选择数据库 mysql_select_db($dbdatabase, $link); //mysql_close($link); } //db_connmysql('password','test'); ?>
<?php //MySQLi连接数据库 function db_connmysqli($dbpassword,$dbdatabase,$dbhost='localhost',$dbname='root',$dbcharset='UTF8'){ $link = mysqli_connect($dbhost,$dbname,$dbpassword) or die(mysqli_error()); $dbVersion = mysqli_get_server_info($link); //设置字符集 mysqli_set_charset($link,$dbcharset); //mysqli_query($link,"SET NAMES gb2312");此方式不推荐; //设置 sql_model if($dbVersion >'5.0.1'){ mysqli_query($link,"SET sql_mode=''"); mysqli_query($link,"SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary");// 防止宽字节注入 } //选择数据库 mysqli_select_db($link,$dbdatabase); //mysqli_close($link); } db_connmysqli('password','test'); ?>
最后更新于 2022年7月8日
相关博文
php连接MySQL数据库代码(原创)