Home > Backend Development > PHP Problem > How to connect to the database and query data through php

How to connect to the database and query data through php

PHPz
Release: 2023-03-28 13:50:35
Original
1365 people have browsed it

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";
?>
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template