PDO Fetch Num


 

PDO Fetch Num

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 Num:

We've discussed in the previous tutorial about PDO::FETCH_ASSOC, similarly PDO provides another function which returns the numerical index of the result unlike the PDO::FETCH_ASSOC which returns the name of the field. To make the differences clear please compare the previous result and the result given below, you'll find that the result of the  current tutorial is in numeric form.

Example:

<?php

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

$host='localhost';

$user='root';

$password='';

try{

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

$sql="select * from student";

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

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

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:

0rose
11

Ads