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