PHP MySQLi
PHP MySQLi
In this article, we will focus on the PHP MySQLi extension, which is used to interact with MySQL databases in PHP. We will provide you with an overview of the extension, how it works, and examples of its use. (在本文中,我们将重点介绍PHP MySQLi扩展,该扩展用于与PHP中的MySQL数据库进行交互。我们将为您提供扩展程序的概述、工作原理及其使用示例。)
Introduction to the MySQLi extension
Introduction to the MySQLi extension (MySQLi扩展简介)
The MySQLi extension is a PHP extension that provides an interface for interacting with MySQL databases in PHP. It is an improved version of the older MySQL extension and provides better security, performance, and functionality. (MySQLi扩展是一个PHP扩展,它提供了一个用于在PHP中与MySQL数据库交互的接口。它是旧版MySQL扩展的改进版本,提供了更好的安全性、性能和功能。)
The MySQLi extension provides two different interfaces for interacting with MySQL databases: an object-oriented interface and a procedural interface. In this article, we will focus on the object-oriented interface.
How to use the MySQLi extension
How to use the MySQLi extension (如何使用MySQLi扩展)
Using the MySQLi extension is very simple. You just need to create a new MySQLi object, which represents a connection to a MySQL database. Here is an example:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
$result = $mysqli->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
$mysqli->close();
?>
In this example, we create a new MySQLi object with the mysqli() constructor, passing in the hostname, username, password, and database name as arguments. We then check if the connection was successful by checking the connect_errno property of the MySQLi object. (在此示例中,我们使用mysqli ()构造函数创建一个新的MySQLi对象,并将主机名、用户名、密码和数据库名称作为参数传递。然后,我们通过检查MySQLi对象的connect_errno属性来检查连接是否成功。)
We then execute a SELECT query to retrieve all rows from a table named users. We iterate over the result set using a while loop and output the name and email of each user. (然后,我们执行SELECT查询,从名为USERS的表中检索所有行。我们使用while循环对结果集进行迭代,并输出每个用户的姓名和电子邮件。)
Finally, we close the MySQLi connection using the close() method of the MySQLi object. (最后,我们使用MySQLi对象的close ()方法关闭MySQLi连接。)
Advanced usage
Advanced usage (高级用法:)
The MySQLi extension provides many other methods for interacting with MySQL databases. For example, you can use the prepare() method to prepare a statement with placeholders, and then execute the statement with different parameter values. Here is an example:
<?php
$stmt = $mysqli->prepare("SELECT name, email FROM users WHERE age > ?");
$stmt->bind_param("i", $age);
$age = 18;
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
$stmt->close();
?>
In this example, we use the prepare() method to prepare a SELECT statement with a placeholder for the age parameter. We then bind a value of 18 to the parameter using the bind_param() method. We execute the statement with the execute() method and retrieve the result set with the get_result() method. We then iterate over the result set and output the name and email of each user. (在此示例中,我们使用prepare ()方法为age参数准备带有占位符的SELECT语句。然后,我们使用bind_param ()方法将值18绑定到参数。我们使用execute ()方法执行语句,并使用get_result ()方法检索结果集。然后,我们对结果集进行迭代,并输出每个用户的姓名和电子邮件。)
Conclusion
Conclusion (小结)
In conclusion, the MySQLi extension is a powerful tool for interacting with MySQL databases in PHP. By understanding how to use the extension and its advanced usage scenarios, you can take advantage of this feature to create powerful and secure database-driven applications in your PHP scripts. (总之, MySQLi扩展是在PHP中与MySQL数据库交互的强大工具。通过了解如何使用扩展及其高级使用方案,您可以利用此功能在PHP脚本中创建功能强大且安全的数据库驱动应用程序。)