Current Location: Home> Latest Articles> Analyze the difference between mysqli_result and PDOStatement

Analyze the difference between mysqli_result and PDOStatement

M66 2025-05-18

In PHP, mysqli_result and PDOStatement are both result set objects for database operations, but there are some significant differences between them. This article will analyze the differences between the two to help developers better understand and choose which method to use to interact with MySQL database.

1. Database extension

  • mysqli (MySQL Improved) is an extension designed specifically for MySQL databases, providing rich object-oriented and procedural interfaces. mysqli_result is a class in the mysqli extension that handles the result set returned by the query.

  • PDO (PHP Data Objects) is a database abstraction layer that provides a unified interface to operate multiple database types. PDOStatement is a class used in the PDO extension to process SQL query results. Although PDO can support multiple database types, its MySQL implementation is also very commonly used.

2. Usage comparison

  • mysqli_result

    In mysqli , when you execute a query, the returned result set is stored as a mysqli_result object. You can use various methods of mysqli_result to get the results.

     $mysqli = new mysqli("localhost", "user", "password", "database");
    $result = $mysqli->query("SELECT * FROM users");
    
    if ($result instanceof mysqli_result) {
        while ($row = $result->fetch_assoc()) {
            echo $row['name'];
        }
    }
    

    In this example, $result is a mysqli_result object. By calling fetch_assoc() , you can get the result in the form of an associative array for each row.

  • PDOStatement

    In PDO , query results are usually returned by the PDOStatement object. You can get the query results through the fetch() method of PDOStatement .

     $pdo = new PDO("mysql:host=localhost;dbname=database", "user", "password");
    $stmt = $pdo->query("SELECT * FROM users");
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $row['name'];
    }
    

    In this example, $stmt is a PDOStatement object, you can use fetch() to traverse the results.

3. Main differences

  • Functional range

    mysqli is designed for MySQL databases and provides more specific functions for MySQL, such as stored procedures, transactions, etc. PDO is a database abstraction layer that allows developers to access different types of databases using the same code.

  • API Design

    mysqli provides two interfaces: object-oriented and procedural, but PDO only supports object-oriented programming. Therefore, PDOStatement is object-oriented and does not support procedural programming.

  • Result Set Processing

    mysqli_result has more options to get results, such as fetch_assoc() , fetch_row() , fetch_object() , etc., allowing developers to choose the right way according to different needs. The fetch() method of PDOStatement is more concise, returning only one row of data.

  • Database support

    mysqli can only be used for MySQL databases, while PDO supports a variety of database systems, such as MySQL, PostgreSQL, SQLite, etc., which makes PDO more flexible in application scenarios that require cross-platform or cross-database.

  • Transaction processing

    PDO supports transaction operations, and although mysqli also supports transactions, its transaction processing method is not as flexible as PDO . PDO also supports processing transactions through beginTransaction() , commit() and rollBack() , which makes it more convenient for developers to handle transactions.

4. Performance differences

In terms of performance, the difference between mysqli and PDO is usually very small, mainly depending on the specific usage scenario. Generally speaking, mysqli has a slightly more performance advantage when interacting with MySQL databases because it is designed for MySQL, and the versatility of PDO may make it slightly inferior in certain specific operations.

However, in modern development, performance differences are usually not the main consideration for choosing to use mysqli or PDO , but are more based on database type and development needs.

5. Security

In terms of security, both mysqli and PDO support the use of prepared statements to prevent SQL injection. PDO supports multiple databases, so its preprocessing statements are also more general and can adapt to different databases.

For example, when using PDO , the preprocessing statement is written as follows:

 $stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);

Similarly, mysqli also supports preprocessing statements:

 $stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();

6. Summary

  • If you only use MySQL databases and need more MySQL features, mysqli may be a better choice.

  • If you need cross-database support or pay more attention to code universality, PDO will be more suitable.

  • In terms of security and performance, both mysqli and PDO can provide relatively high guarantees, and developers can flexibly choose according to their needs.

7. URL example

If your project involves URL operations, make sure to change the URL's domain name to m66.net , for example: