Mysql 简明教程
MySQL - PHP Syntax
各种编程语言如 PERL、C、C++、JAVA、PHP 等被用作客户端程序,请求对 MySQL Server 进行查询执行。在这些语言中,PHP 因其 Web 应用开发能力而最受欢迎。
Various programming languages like PERL, C, C++, JAVA, PHP, etc. are used as client programs to request query executions on a MySQL Server. Out of these languages, PHP is the most popular one because of its web application development capabilities.
PHP 库就像 Web 开发人员的工具箱,提供了预先构建的函数和代码片段来简化常见的任务。它通过为处理数据库、处理表单和管理文件等任务提供现成的解决方案来节省时间和精力。开发人员可以轻松地将这些库包含在 PHP 项目中,以提高效率并创建强大的 Web 应用程序。
A PHP library is like a toolbox for web developers, providing pre-built functions and code snippets to simplify common tasks. It saves time and effort by offering ready-made solutions for tasks such as handling databases, processing forms, and managing files. Developers can easily include these libraries in their PHP projects to boost efficiency and create robust web applications.
PHP MySQLi Library
MySQL PHP 连接器通常被称为 MySQLi,它支持 PHP 脚本和 MySQL 数据库之间的通信。它提供了一组函数和方法,允许 PHP 应用程序连接、查询和操作 MySQL 数据库中的数据,在 PHP Web 开发中提供高效且安全的数据库交互。
The MySQL PHP connector, often referred to as MySQLi, enables communication between PHP scripts and MySQL databases. It provides a set of functions and methods that allow PHP applications to connect, query, and manipulate data in MySQL databases, providing efficient and secure database interactions in PHP web development.
本教程重点介绍在各种环境中使用 MySQL。如果您对 PHP 中的 MySQL 感兴趣,那么您可以考虑阅读 PERL 教程。
This tutorial focuses on using MySQL in a various environments. If you are interested in MySQL with PERL, then you can consider reading the PERL Tutorial.
How to Install MySQLi
要在 Windows 中安装 MySQLi,您可以按照以下一般步骤操作 -
To install MySQLi on Windows, you can follow these general steps −
Download PHP:
Download PHP:
-
Download the latest version of PHP from the official PHP website ([role="bare"]https://www.php.net/downloads.php).
-
Choose the Windows version that matches your system architecture (e.g., 32-bit or 64-bit).
-
Download the ZIP file for the Thread Safe or Non-Thread Safe version, depending on your needs.
Extract the ZIP File:
Extract the ZIP File:
-
Extract the contents of the downloaded ZIP file to a location on your computer (e.g., C:\php).
Configure PHP:
Configure PHP:
-
In the extracted PHP folder, find the "php.ini-development" file.
-
Copy and rename it to "php.ini".
-
Open "php.ini" in a text editor (e.g., Notepad) and find the line: ";extension=mysqli". Remove the semicolon (;) at the beginning of the line to uncomment it: "extension=mysqli".
-
Save the php.ini file.
Set Environment Variables:
Set Environment Variables:
-
Add the PHP installation directory to the system’s PATH environment variable. This allows you to run PHP from any command prompt.
-
To do this, right-click on "This PC" or "Computer" on your desktop or in File Explorer, select "Properties," and click on "Advanced system settings." Then, click on the "Environment Variables" button. In the "System variables" section, select the "Path" variable and click "Edit." Add the path to your PHP installation directory (e.g., C:\php).
Restart your Web Server:
Restart your Web Server:
-
If you are using a web server like Apache or Nginx, restart it to apply the changes.
Verify Installation:
Verify Installation:
-
Create a PHP file with the following content and save it in your web server’s document root (e.g., C:\Apache24\htdocs for Apache):
<?php
phpinfo();
?>
PHP Functions to Access MySQL
PHP 提供了多种函数来访问 MySQL 数据库,以及在 MySQL 数据库中操纵数据记录 −
PHP provides various functions to access the MySQL database and to manipulate the data records inside the MySQL database −
Basic Example
以下是使用 PHP 和 MySQLi 连接到 MySQL 数据库、执行查询、处理结果以及关闭连接的步骤 −
Following are the steps to connect to a MySQL database, execute a query, process the results, and close the connection using PHP and MySQLi −
-
Define the parameters needed to connect to your MySQL database, such as '$dbhost' (host name), '$dbuser' (username), '$dbpass' (password), and '$dbname' (database name).
-
Create a new instance of the 'mysqli' class to establish a connection to the MySQL database.
-
Use the 'query' method of the 'mysqli' object to execute a MySQL query.
-
Fetch and process the results returned by the query.
-
Close the connection to the MySQL database when you are done.
以下示例显示了 PHP 的通用语法,用于调用任何 MySQL 查询。
The following example shows a generic syntax of PHP to call any MySQL query.
<html>
<head>
<title>PHP with MySQL</title>
</head>
<body>
<?php
// Include database connection parameters
$dbhost = "localhost";
$dbuser = "your_username";
$dbpass = "your_password";
$dbname = "your_database";
// Establish a connection to MySQL
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute a MySQL query
$sql = "SELECT * FROM your_table";
$result = $mysqli->query($sql);
if (!$result) {
die("Error: " . $mysqli->error);
}
// Process the query results
while ($row = $result->fetch_assoc()) {
// Process each row of data
echo "ID: " . $row["id"] . " Name: " . $row["name"] . "<br>";
}
// Close the database connection
$mysqli->close();
?>
</body>
</html>