Skip to content
PHP Logo

What are some alternative methods to mysql_fetch_array for fetching data from a MySQL database in PHP?

When fetching data from a MySQL database in PHP, an alternative method to mysql_fetch_array is to use the mysqli_fetch_array function. This function is part of the improved MySQL extension (mysqli) in PHP and provides similar functionality to mysql_fetch_array but with better performance and security features.

php
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Execute a query
$result = $mysqli->query("SELECT * FROM table");

// Fetch data using mysqli_fetch_array
while ($row = mysqli_fetch_array($result)) {
    // Process each row of data
    echo $row['column_name'];
}

// Close the connection
$mysqli->close();