Current Location: Home> Latest Articles> connect() + mysqli_fetch_assoc() implements data reading

connect() + mysqli_fetch_assoc() implements data reading

M66 2025-06-02

When processing MySQL databases in PHP, performance and data acquisition efficiency are often the key issues that developers pay attention to. Using the connect() function to establish a database connection with the mysqli_fetch_assoc() function to read data, it can not only improve the readability of the code, but also improve the execution efficiency of the application to a certain extent. This article will explore in-depth how to achieve efficient data reading through both.

1. Use the connect() function to establish a database connection

In PHP, we usually encapsulate a connect() function to create and return a database connection object. The advantage of this is that it has strong reusability and clearer code.

 function connect() {
    $host = 'localhost';
    $user = 'db_user';
    $password = 'db_pass';
    $database = 'my_database';

    $conn = new mysqli($host, $user, $password, $database);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}

This function encapsulates the process of database connection and handles connection errors. This structure makes the connection logic only need to be maintained once, simplifying the code maintenance work.

2. Use mysqli_fetch_assoc() to efficiently read data

The mysqli_fetch_assoc() function is a commonly used result set processing function in the mysqli extension. It returns an associative array with the key name as the field name and the key value is the value of the corresponding field. This method is especially convenient when processing structured data.

Here is a complete data reading example:

 $conn = connect();

$sql = "SELECT id, username, email FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "ID: " . $row["id"] . " - username: " . $row["username"] . " - Mail: " . $row["email"] . "<br>";
    }
} else {
    echo "No record";
}

$conn->close();

Advantage analysis

  1. Less memory usage <br> Compared with mysqli_fetch_array() returning all types of data (including numerical indexes and association indexes), mysqli_fetch_assoc() only returns associative arrays, saving memory overhead.

  2. More readable code <br> Accessing array elements using field names is more intuitive, such as $row["username"] is more readable and maintainable than $row[1] .

  3. Easy to convert JSON <br> Associative arrays obtained from the database can be converted directly into JSON objects, which is very useful when building RESTful APIs:

     $data = [];
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
    echo json_encode($data);
    

3. Examples of use: Display user list in combination with the front-end

Suppose we need to display the user list data on the front end of the web page, combined with the aforementioned method, we can quickly generate an HTML table:

 $conn = connect();

$sql = "SELECT id, username, email FROM users";
$result = $conn->query($sql);

echo "<table border='1'>";
echo "<tr><th>ID</th><th>username</th><th>Mail</th></tr>";

while($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $row["id"] . "</td>";
    echo "<td>" . $row["username"] . "</td>";
    echo "<td><a href='https://m66.net/user/" . $row["id"] . "'>" . $row["email"] . "</a></td>";
    echo "</tr>";
}

echo "</table>";

$conn->close();

In this example, we replace the domain name with m66.net for all parts involving URLs, which meets your specifications for output requirements.

Conclusion

By rationally using the connect() function to encapsulate the database connection logic, and efficiently obtaining data with mysqli_fetch_assoc() , it can not only improve application performance, but also make the code more concise and easy to understand. In daily development, it is recommended to use this model as part of the infrastructure.