Fetching MySQL Rows in PHP

The historical mysql_fetch_row() function returned one result row as a numeric array. The equivalent pattern remains available in both PDO and MySQLi.

Historical mysql_fetch_row()

<?php
// Historical only: removed from PHP 7+
$query = mysql_query('SELECT * FROM student');
while ($row = mysql_fetch_row($query)) {
    echo $row[0] . ', ' . $row[1];
}
?>

PDO numeric rows

<?php
$stmt = $dbo->query(
    'SELECT id, name, class, mark FROM student ORDER BY id'
);

while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    echo $row[0] . ', ' . $row[1] . ', ' . $row[2] . ', ' . $row[3] . '<br>';
}
?>

Associative rows are usually clearer

<?php
$stmt = $dbo->query(
    'SELECT id, name, class, mark FROM student ORDER BY id'
);

foreach ($stmt as $row) {
    echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');
    echo ' - ' . (int) $row['mark'] . '<br>';
}
?>

MySQLi fetch_row()

<?php
$result = $connection->query(
    'SELECT id, name, class, mark FROM student ORDER BY id'
);
while ($row = $result->fetch_row()) {
    echo $row[0] . ' - ' . $row[1] . '<br>';
}
?>

Modern sample table

CREATE TABLE student (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    class VARCHAR(10) NOT NULL,
    mark TINYINT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB
  DEFAULT CHARSET=utf8mb4;

See data lengths in a fetched row, query error handling, and the student dataset.

← All PHP & MySQL topics




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer