Home Backend Development PHP Tutorial Some summaries and examples about php mysqli functions (3)

Some summaries and examples about php mysqli functions (3)

Aug 16, 2019 pm 06:07 PM
mysqli

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.

Recommended video tutorial: https://www.php.cn/course/list/29/type/4.html

Understanding of phpMysqli functions:

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: http://www.php.net/manual/en/mysqli.installation.php

For details about running configuration Detailed information address: http://www.php.net/manual/en/mysqli.configuration.php

3. Introduction to php related functions

1, mysqli_change_user(connection,username,password,newdbname)

Description: Change the connected database

Instance:

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;
$newdbname = &#39;test&#39;;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接数据库失败!&#39;.mysqli_connect_error());
}
//连接成功,更改连接的数据库
if(mysqli_change_user($conn,$username,$password,$newdbname)) {
echo &#39;连接成功!&#39;;
}else {
echo &#39;连接失败!&#39;;
}
//关闭连接
mysqli_close($conn);
?>
Copy after login

2, mysqli_character_set_name() function

Description: Returns the default character set for connecting to the database.

Example:

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username= &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;
 
//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(!$conn) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//连接成功,获取默认字符集
$charset = mysqli_character_set_name($conn);
echo &#39;默认字符集为:&#39;.$charset;

//关闭连接
mysqli_close($conn);
?>
Copy after login

3, mysqli_data_seek()Function

Description: Adjust the result pointer to any row in the result set.

<?php
//配置数据库相关信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//连接成功,利用移动指针操作完成查找固定行
//定义sql语句
$sql = "select * from demo";
$result = mysqli_query($conn,$sql);
//未改变指针位置时
$roww = mysqli_fetch_row($result);
print_r($roww[0]);
echo "<br />";
if($result) {
mysqli_data_seek($result,2);
//取出数据
$row = mysqli_fetch_row($result);
print_r($row[0]);
}else {
die(&#39;连接失败!&#39;);
}
//释放结果集内存
mysqli_free_result($result);
//关闭连接
mysqli_close($conn);
?>
Copy after login

4, mysqli_free_result() function

Description: Release the result set memory.

Example:

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//连接成功,执行sql语句
$sql = "select * from demo";
if($result = mysqli_query($conn, $sql)) {
while($row = mysqli_fetch_row($result)) {
print_r($row);
echo "<br>";
}
}else {
die(&#39;执行失败!&#39;);
} 
//释放结果集内存
mysqli_free_result($result);
//关闭连接
mysqli_close($conn);
?>
Copy after login
Copy after login

5, mysqli_fetch_row()Function

Description: From the result set Takes a row and returns it as an enumeration array.

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
if(mysqli_connect_errno($conn)) {
die(&#39;连接失败!&#39;.mysqli_connect_error());
}
//连接成功,执行sql语句
$sql = "select * from demo";
if($result = mysqli_query($conn, $sql)) {
while($row = mysqli_fetch_row($result)) {
print_r($row);
echo "<br>";
}
}else {
die(&#39;执行失败!&#39;);
} 
//释放结果集内存
mysqli_free_result($result);
//关闭连接
mysqli_close($conn);
?>
Copy after login
Copy after login

The above is a detailed introduction and examples of some functions summarized this time. I hope it can help everyone.

Recommended related articles: https://www.php.cn/php-weizijiaocheng-428659.html

The above is the detailed content of Some summaries and examples about php mysqli functions (3). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to PHP Fatal error: Call to undefined function mysqli_connect() Solution to PHP Fatal error: Call to undefined function mysqli_connect() Jun 23, 2023 am 09:40 AM

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PHP PDO vs. mysqli: compare and contrast PHP PDO vs. mysqli: compare and contrast Feb 19, 2024 pm 12:24 PM

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

What should I do if php cannot connect to mysqli? What should I do if php cannot connect to mysqli? Nov 09, 2022 am 10:07 AM

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

What is the running file of mysql What is the running file of mysql Apr 11, 2023 am 10:38 AM

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

Solution to PHP Fatal error: Call to undefined method mysqli::prepare() Solution to PHP Fatal error: Call to undefined method mysqli::prepare() Jun 23, 2023 am 11:21 AM

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

Solution to PHP Fatal error: Call to undefined function mysqli_stmt_bind_param() Solution to PHP Fatal error: Call to undefined function mysqli_stmt_bind_param() Jun 23, 2023 am 10:43 AM

When developing websites using PHP, database operations are very common. MySQLi is an extension commonly used in PHP to operate MySQL databases. It provides a relatively complete object-oriented interface, procedural interface, and supports operations of prepared statements. But sometimes when we use mysqli's prepared statements, we will encounter such an error: PHPFatalerror:Calltoundefinedfunctionmysqli_stmt_bin

See all articles