Home Tutorial Php Phppdo Tutorial PDO Fetch Num

 
 

PDO Fetch Num
Posted on: April 5, 2010 at 12:00 AM
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

Related Tags for PDO Fetch Num:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.