Home  >  Article  >  Backend Development  >  Detailed explanation of php mysqli operation mysqli_connect to connect to the database instance

Detailed explanation of php mysqli operation mysqli_connect to connect to the database instance

怪我咯
怪我咯Original
2017-01-23 14:43:339252browse

PHP must first connect to the specified database to operate the database. The mysql_connect function we used before is no longer supported by some PHP versions. Currently, the editor uses the mysqli_connect function to link to the database. The usage is the same as mysql Basically similar. This article introduces how to use the mysqli_connect function and examples. Friends who need it can refer to

## Recommended related mysql video tutorials: "mysql tutorial"

Introduction to mysqli_connect

php mysqli

_connect is used to connect to the mysql server. This function has multiple parameters and the syntax is as follows

mysqli_connect(host,username,password,dbname,port,socket);

The parameters are as follows:

host Optional. Specify the hostname or IP address.

username Optional. Specifies the MySQL username.

password Optional. Specifies the MySQL password.

dbname Optional. Specifies the database to use by default.

port Optional. Specifies the port number to attempt to connect to the MySQL server.

socket Optional. Specifies the socket or named pipe to use.

But we generally only need to know the following four parameters:

host Optional. Specify the hostname or IP address.

username Optional. Specifies the MySQL username.

password Optional. Specifies the MySQL password.

dbname Optional. Specifies the database to use by default. ​

mysql_connect instance

<?php 
$con=mysqli_connect("localhost","wrong_user","my_password","my_db"); 
// 检查连接 
if (!$con) 
{ 
    die("连接错误: " . mysqli_connect_error()); 
} 
?>

If the connection is successful, a MySQL connection ID is returned. If the connection fails, FALSE is returned.

This is the usage of mysqli_connect(), which is very close to mysql_connect. In other words, there is just one more i difference.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn