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:
| 0 | rose |
| 1 | 1 |
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.