Jump to: navigation, search

PHP MySQL PDO Fetch NUM

From w3cyberlearnings

Contents

PHP PDO FETCH NUM

PDO::FETCH_NUM produces a numerical index.

Syntax PDO

$sq = $db->query("SELECT 
         id,
         first_name, 
         last_name,
          email FROM user_infor");

while ($r = $sq->fetch(PDO::FETCH_NUM)) {
	echo $r[0]; // id
        echo "<br/>";
        echo $r[1]; // first name
        echo "<br/>";
        echo $r[2]; // last name
        echo "<br/>";
	echo $r[3]; // email
        echo "<br/>";

}

Example 1

<?php
// fetch as NUM
$dns = 'mysql:host=localhost;dbname=w3cyberlearning';
$user = 'user2000';
$pass = 'password2000';

$db = new PDO($dns, $user, $pass);

$sq = $db->query("SELECT 
         id,
         first_name, 
         last_name,
          email FROM user_infor");

echo '<table border="1">';
echo '<tr>
	<th>Id</th>
	<th>First Name</th>
	<th>Last Name</th>
	<th>Email</th>
</tr>';
// access record as index
while ($r = $sq->fetch(PDO::FETCH_NUM)) {
	echo '<tr>';
	echo '<td>' . $r[0] . '</td>';
	echo '<td>' . $r[1] . '</td>';
	echo '<td>' . $r[2] . '</td>';
	echo '<td>' . $r[3] . '</td>';
	echo '</tr>';
}
echo '</table>';
?>

Output

Php mysql pdo fetch 1.png

Related Links


Navigation
Web
SQL
MISC
References