Home >Backend Development >PHP Tutorial >Some organization and detailed introduction about PHP Mysqli functions (1)

Some organization and detailed introduction about PHP Mysqli functions (1)

王林
王林Original
2019-08-16 11:07:163746browse

Foreword: PHP is a programming language that is relatively easy to get started with, and PHP has a lot of built-in functions. Therefore, it is particularly important to understand and master these built-in functions. Next we will analyze some of the built-in functions of PHP.

I will continue to organize it for you in the future.

Recommend php video tutorial: php tutorial

Recommend mysql video tutorial mysql

Understanding of phpMysqli function:

1. What is php mysqli?

php mysqli = php nysqli improved

The mysqli function allows you to access the database server.

Notice! The mysqli extension is available for mysqli version 4.1.13 or newer.

2. How to use mysqli function?

If you want to use the mysqli function, you must add support for the mysqli extension when compiling php.

For more information about installation, please visit: Installation address visit

For more information about running configuration address:Running configuration visit

3. PHP mysqli function introduction

1.mysqli_connect(host,username,password,dbname,port,socket)

Description : Open a new connection to the mysql server. What is returned is a connection object representing the database.

Example:

<?php
$conn = mysqli_connect("localhost","username","password","dbname","port");
//检查连接
if(!$conn) {
die("连接错误:".mysqli_connect_error());
}
?>

Some organization and detailed introduction about PHP Mysqli functions (1)

2、mysqli_connect_error()

Description: The function returns the error description of the last connection error. What is returned is a string describing the error, or NULL if no error occurs.

Example:

<?php
$conn = mysqli_connect("localhost","username","password","dbname");
//检查连接
if(!$conn) {
die(mysqli_connect_error());
}
?>

3, mysqli_errno(connection)

Description: The function returns the last error of the most recently called function code. If there is no error, return 0

Example:

<?php
//连接数据库
$conn = mysqli_connect("localhost","username","password","dbname"));
//检查连接
if(mysqli_connect_errno($conn)) {
echo "连接数据库失败:".mysqli_connect_error();
}
//连接成功,执行查询语句
if(!mysqli_query($conn, "INSERT INTO ... (...) VALUES (...)")) {
//查询失败,返回错误代码
echo("查询失败:".mysqli_errno($conn));
}
//关闭连接
mysqli_close($conn);
?>

4, mysqli_connect_errno()

Description: The function returns the last time The error code of the connection, or 0 if there is no error.

Example:

<?php
//连接数据库
$conn = mysqli_connect("localhost","username","password","dbname");
//检查连接
if(!$conn) {
die("连接失败:".mysqli_connect_errno());
}
?>

Some organization and detailed introduction about PHP Mysqli functions (1)

5、mysqli_close(connection);

Description: Close the previously opened database connection.

Example:

<?php
//连接数据库
$conn = mysqli_connect("localhost","username","password","dbname");
//php代码
//关闭连接
mysqli_close($conn);
?>

The above are the uses of some functions that I have collected for you. I will continue to organize the functions for you in the future to increase your memory of the functions. Thanks!

The above is the detailed content of Some organization and detailed introduction about PHP Mysqli functions (1). For more information, please follow other related articles on the PHP Chinese website!

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