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 = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; $newdbname = 'test'; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接数据库失败!'.mysqli_connect_error()); } //连接成功,更改连接的数据库 if(mysqli_change_user($conn,$username,$password,$newdbname)) { echo '连接成功!'; }else { echo '连接失败!'; } //关闭连接 mysqli_close($conn); ?>
2
, mysqli_character_set_name() function
Description: Returns the default character set for connecting to the database.
Example:
<?php //配置数据库信息 $localhost = 'localhost'; $username= 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(!$conn) { die('连接失败!'.mysqli_connect_error()); } //连接成功,获取默认字符集 $charset = mysqli_character_set_name($conn); echo '默认字符集为:'.$charset; //关闭连接 mysqli_close($conn); ?>
3, mysqli_data_seek()
Function
Description: Adjust the result pointer to any row in the result set.
<?php //配置数据库相关信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); //检查连接 if(mysqli_connect_errno($conn)) { die('连接失败!'.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('连接失败!'); } //释放结果集内存 mysqli_free_result($result); //关闭连接 mysqli_close($conn); ?>
4, mysqli_free_result() function
Description: Release the result set memory.
Example:
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); if(mysqli_connect_errno($conn)) { die('连接失败!'.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('执行失败!'); } //释放结果集内存 mysqli_free_result($result); //关闭连接 mysqli_close($conn); ?>
5, mysqli_fetch_row()
Function
Description: From the result set Takes a row and returns it as an enumeration array.
<?php //配置数据库信息 $localhost = 'localhost'; $username = 'zmz'; $password = '20040315'; $dbname = 'zmz'; $port = 3306; //连接数据库 $conn = mysqli_connect($localhost,$username,$password,$dbname,$port); if(mysqli_connect_errno($conn)) { die('连接失败!'.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('执行失败!'); } //释放结果集内存 mysqli_free_result($result); //关闭连接 mysqli_close($conn); ?>
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!