Home  >  Article  >  Backend Development  >  How to get the first few records when operating the database in PHP?

How to get the first few records when operating the database in PHP?

王林
王林Original
2024-03-22 08:12:041136browse

How to get the first few records when operating the database in PHP?

How to get the first few records when PHP operates the database?

When performing database operations, sometimes we need to obtain the first few records in the database for display or other operations. In PHP, we can use SQL statements combined with PHP code to achieve this function. Below we will introduce how to use mysqli and PDO to obtain the first few records.

Use mysqli to get the first few records

First, we need to connect to the database and execute the SQL statement, and then use the mysqli_fetch_array() or mysqli_fetch_assoc() function to get the query results.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Check for phrases
$sql = "SELECT * FROM table_name LIMIT 5";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output Data
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

In the above code, the first 5 records in the query table are printed out.

Use PDO to get the first few records

PDO is another commonly used database operation extension. By operating the database through PDO, we can also execute SQL statements through PDO::query(), and then use the PDOStatement::fetch() method to obtain the query results.

// Connect to the database
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Check for phrases
    $sql = "SELECT * FROM table_name LIMIT 5";
    
    $stmt = $conn->query($sql);
    
    //Set the result set as an associative array
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    
    while ($row = $stmt->fetch()) {
        echo "id: " . $row['id'] . " - Name: " . $row['name'] . "<br>";
    }
} catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$conn = null;

The above code can also obtain the first 5 records in the table and output them.

Through the above sample code, we can see how to use mysqli and PDO to get the first few records in the database in PHP. Choosing the appropriate method according to the actual situation can operate the database more efficiently.

The above is the detailed content of How to get the first few records when operating the database in 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