PDO Fetch Both


 

PDO Fetch Both

In this tutorial we will study about PDO Fetch Num in PHP, how to access MySQL using PDO Fetch Num function, How to retrieve data etc. Examples will make this clearer.

In this tutorial we will study about PDO Fetch Num in PHP, how to access MySQL using PDO Fetch Num function, How to retrieve data etc. Examples will make this clearer.

PDO Fetch Both:

In the previous two examples we have seen the two methods of  PDO extension, with which we can get the numeric indices and character indices. But what if we need both type of indices (character type and numeric type), PDO provides the solution as PDO::FETCH_BOTH produces, with the help of this function we can achieve both type of output. PDO::FETCH_BOTH produces both numerical and associative indices and as per the requirement we can use both or either of these.

Example:

<?php

header('Content-type:text/html');

$host='localhost';

$user='root';

$password='';

try

{

$dbh=new PDO("mysql:host=$hostname;dbname=ajax",$user,$password);

//echo "Connected to database";

$sql="select * from student";

$stmt=$dbh->query($sql);

$result=$stmt->fetch(PDO::FETCH_BOTH);

echo "<table border=1>";

foreach($result as $key=>$val)

{

echo "<tr><td>".$key."</td>";

echo "<td>".$val."</td></tr>";

}

echo "</table>";

$dbh=null;

}

catch(PDOException $c)

{

echo $c->getMessage();

}

?>

Output:

namerose
0rose
roll1
11

Ads