Home  >  Article  >  PHP operates mysqli database connection

PHP operates mysqli database connection

无忌哥哥
无忌哥哥Original
2018-06-28 11:23:141880browse

* 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 &#39;<h1>连接成功</h1>&#39;;

//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);
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
Previous article:How to use session in phpNext article:How to use session in php