* mysqli database connection
* Steps:
* 1. Create connection parameters
* 2. Call the connection function and return the connection resource
* 3. Determine whether the connection is successful
* 4. Select the database
* 5. Set the default character set
* Functions used
* 1 . mysqli_connect($host,$user,$pass)
* 2. mysqli_connect_errno($db)
* 3. mysqli_connect_error($db)
* 4. mysqli_select_db ($dbName)
* 5. mysqli_set_charset('utf8')
//1. Create connection parameters
//Create connection parameters: Because the connection parameters are not frequent changes, so it is recommended to use the constant
define ('DB_HOST', 'localhost'); define ('DB_USER', 'root'); define ('DB_PASS', 'root'); define ('DB_NAME', 'php'); define ('DB_CHAR', 'utf8');
//2. Call the connection function, the mysqli object will be returned if successful, and false will be returned if failed
$db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS); var_dump($db);die();
//3. Is the test connection successful?
//Failed connection will definitely return an error number. You can judge based on the number, or you can use whether $db is false to judge.
if (mysqli_connect_errno($db)) { exit('连接失败'.mysqli_connect_error($db)); } echo '<h1>连接成功</h1>';
//4. Select the database to be operated
mysqli_select_db($db, DB_NAME);
/ /5. Set the default character set
mysqli_set_charset($db, DB_CHAR);