Home  >  Article  >  Backend Development  >  How to query mysql natively in php

How to query mysql natively in php

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-08-10 13:04:511464browse

The operation of php native query mysql is: 1. Create a php sample file; 2. Connect to the database; 3. Check whether the link is successful; 4. Execute the query statement; 5. Process the query results; 6. Close the database Just connect.

How to query mysql natively in php

Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.

In PHP, you can query the MySQL database in a native way. The following is a simple sample code:

<?php
// 连接数据库
$servername = "localhost";
$username = "你的用户名";
$password = "你的密码";
$dbname = "你的数据库名";
conn=newmysqli(conn = new mysqli(conn=newmysqli(servername, username,username, username,password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
// 执行查询语句
$sql = "SELECT * FROM 表名";
result=result = result=conn->query($sql);
// 处理查询结果
if ($result->num_rows > 0) {
    while (row=row = row=result->fetch_assoc()) {
        // 处理每一行数据
        echo "ID: " . row["id"].",名称:".row["id"] . ", 名称: " . row["id"].",名称:".row["name"];
        echo "<br>";
    }
} else {
    echo "没有匹配的数据";
}
// 关闭数据库连接
$conn->close();
?>

Please pay attention to replacing "your username", "your password", "your Fields such as "database name" and "table name" are your own actual information. You can modify the query statement and the way the results are processed as needed.

This is just a simple example, more complex queries may require the use of prepared statements or other further processing. However, when querying a MySQL database using native methods, ensure that the input data is properly validated and filtered to prevent SQL injection attacks.

The above is the detailed content of How to query mysql natively in php. 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