When developing web applications, we often need to interact with the database. And database connections are a critical part of connecting web applications and data stores. In this article, we will explore how to make a database connection and query the database using PHP.
First, we need to make sure we have PHP and MySQL installed. Install them if you haven't already.
The following is a simple PHP script that can connect to a MySQL database:
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>
In the above code, we first define the server name, username and password. Then we use the new mysqli()
function to create a new connection to the database. If the connection fails, we use the die()
function to output an error message. Otherwise, we output a successful connection message.
Now, let’s look at an example of how to query a database using PHP. Suppose we have a table called "users" with the following three fields: id, name, and age. The following is a simple PHP script that can query this table and output the results:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检测连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // 查询数据 $sql = "SELECT id, name, age FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?>
In the above code, we first define the server name, username, password and database name. Then we create a new connection. In this example, we pass the database name as the fourth parameter to the new mysqli()
function. Next, we query the users
table using the SELECT
statement and store the results in the variable $result
. If the number of query result rows is greater than 0, we use while
to loop through each row. In each row, we use the fetch_assoc()
method to store the row's values in an associative array $row
and output the values. Finally, we close the connection using the $conn->close()
method.
Summary:
In this article, we discussed how to use PHP for MySQL database connection and query. We first wrote a simple connection script and output an error message if the connection failed or a success message. Next, we wrote a query script to query the table using the SELECT
statement and output the query results when the number of result rows is greater than 0. Also note that we use the $conn->close()
method to close the connection after completing the query. Have fun using PHP to query your database!
The above is the detailed content of How to connect to the database and query data through php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
