PHP connection to a MySQL database code example

PHP connection to a MySQL database code example

<?php
//MySQLi connects to the database
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);
//Sets the character set
mysqli_set_charset($link,$dbcharset);
//mysqli_query($link,"SET NAMES gb2312");This method is not recommended;
//set 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");// Prevents wide byte injection
}
//Select the database
mysqli_select_db($link,$dbdatabase);
//mysqli_close($link);
}
db_connmysqli('password','test');
?>

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *