Jump to: navigation, search

PHP MySQL PDO Fetch BOTH

From w3cyberlearnings

Contents

PHP PDO FETCH BOTH

PDO::FETCH_BOTH fetches both numerical and associative indexes.

Syntax PDO::FETCH_BOTH


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

while ($r = $sq->fetch(PDO::FETCH_BOTH)) {
	print_r($r);
}

Example 1

<?php
// fetch as both numerical and associative indexes
$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");

while ($r = $sq->fetch(PDO::FETCH_BOTH)) {
	print_r($r);
}
?>

Output


Array
(
    [id] => 1
    [0] => 1
    [first_name] => bob
    [1] => bob
    [last_name] => lema
    [2] => lema
    [email] => [email protected]
    [3] => [email protected]
)
Array
(
    [id] => 2
    [0] => 2
    [first_name] => Alex
    [1] => Alex
    [last_name] => Gate
    [2] => Gate
    [email] => [email protected]
    [3] => [email protected]
)
Array
(
    [id] => 3
    [0] => 3
    [first_name] => George
    [1] => George
    [last_name] => Washington
    [2] => Washington
    [email] => [email protected]
    [3] => [email protected]
)


Related Links


Navigation
Web
SQL
MISC
References