The historical mysql_fetch_row() function returned one result row as a numeric array. The equivalent pattern remains available in both PDO and MySQLi.
<?php
// Historical only: removed from PHP 7+
$query = mysql_query('SELECT * FROM student');
while ($row = mysql_fetch_row($query)) {
echo $row[0] . ', ' . $row[1];
}
?>
<?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>';
}
?>
<?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>';
}
?>
<?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>';
}
?>
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.