Home  >  Article  >  Backend Development  >  How to query multiple data from mysql using php

How to query multiple data from mysql using php

PHPz
PHPzOriginal
2023-04-04 14:34:20697browse

When using PHP to query the MySQL database, it is often necessary to query multiple pieces of data, and these data may have different data types. This requires developers to think carefully when writing code to avoid unnecessary errors.

This article will introduce how to use PHP to query multiple pieces of data from mysql, helping readers quickly master this skill.

  1. Connecting to the database

In PHP, to query the MySQL database, you must first establish a connection with it. This can be achieved using the following code:

$con = mysqli_connect("localhost", "username", "password", "database_name");
if (!$con) {
    die("连接失败: " . mysqli_connect_error());
}

where localhost is the local server address, username and password are the username and password for logging in to MySQL, database_name is the name of the database to be connected.

  1. Query multiple pieces of data

After you have a connection instance, you can use the MySQLi function in PHP to query multiple pieces of data. The following is the basic format of the query statement:

$query = "SELECT column1, column2, ... FROM table_name WHERE condition";
$result = mysqli_query($con, $query);

In this syntax, column1, column2, etc. are the column names to be queried, table_name is the name of the data table to be queried, and condition is the query condition. Query statements can be executed using the mysqli_query() function, and the returned results are stored in the $result variable.

  1. Handling different data types

When the queried data contains different data types, developers need to pay attention to the type conversion of the data. The following are some common data types:

  • Integer type: used to represent numbers and can be used directly.
  • String type: used to represent text or characters, enclosed in single quotes or double quotes and assigned to variables.
  • Boolean type: used to represent true or false and can be used directly.
  • Floating point type: used to represent decimals, with decimal points.

For example, query the id and name fields in the user table. Their data types are integer and string respectively. type. The query can be completed using the following code:

$query = "SELECT id, name FROM user WHERE age > 18";
$result = mysqli_query($con, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $id = $row["id"];            // $id是整型
    $name = $row["name"];        // $name是字符串型
}

Of course, the above are just basic data types. In actual development, more data types may be encountered, and developers need to handle them according to specific circumstances.

  1. Close the database connection

Finally, you must not forget to close the connection with MySQL to avoid taking up too many resources. The following code can complete the shutdown:

mysqli_close($con);

Complete code example:

$con = mysqli_connect("localhost", "username", "password", "database_name");
if (!$con) {
    die("连接失败: " . mysqli_connect_error());
}

$query = "SELECT id, name FROM user WHERE age > 18";
$result = mysqli_query($con, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $id = $row["id"];            // $id是整型
    $name = $row["name"];        // $name是字符串型
}

mysqli_close($con);

Through the introduction of this article, readers have learned how to use PHP to query MySQL multiple data types. In actual development, developers need to use it flexibly according to specific situations.

The above is the detailed content of How to query multiple data from mysql using 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