Php PDO Fetch methods - FETCH_ASSOC,FETCH_NUM,FETCH_BOTH,FETCH_OBJ

Php PDO has fetch methods which are explain as below We generally use mysql database methods for php to retrieve data from database.Like mysql_fetch_assoc ,mysql_fetch_row ,mysql_fetch_array ,mysql_fetch_obj.

 In Php PDO  we have to use different methods to fetch data from database.

Lets consider the connection and we are fetching books from books table
<?php
    // database connection
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    $sql = "SELECT title FROM books ORDER BY title";
    $query = $conn->query($sql);

 ?> 


PDO fetch method FETCH_ASSOC

Example : 
<?php
    // Fetch using associative array.This method is similar to mysql_fetch_assoc
    $query->setFetchMode(PDO::FETCH_ASSOC);
    while($result = $query->fetch()){
    print_r($result);
}
?>
Result:
Array ( [title] => Book 1)
Array ( [title] => Book 2)


PDO fetch method FETCH_NUM

Example :
 <?php
    // Fetch as numeric array.
    $query->setFetchMode(PDO::FETCH_NUM);
    while($result = $query->fetch()){
    print_r($result);
}
?>
Result:
Array ( [0] => Book 1 )
Array ( [0] => Book 2 )



PDO fetch method FETCH_BOTH

Example : 
<?php
    // Fetch as associative array and numeric array both.This method 
       is similar to mysql_fetch_array 
    $query->setFetchMode(PDO::FETCH_BOTH);
    while($result = $query->fetch()){
    print_r($result);
}
?>
Result:
Array ( [title] => Book 1 [0] => Book 1 )
Array ( [title] => Book 2 [0] => Book 2 )

PDO fetch method FETCH_OBJ

Example :
<?php
    // Fetch as object.This method is similar to mysql_fetch_obj 
    $query->setFetchMode(PDO::FETCH_OBJ);
    while($result = $query->fetch()){
    print_r($result);
}
?>
Result :
stdClass Object
(
[title] =>Book 1
)
stdClass Object
(
[title] =>Book 2
)

13 comments:

Anonymous said...

Thanks a lot, i didn't understand why i had two values with FETCH_BOTH method.....

Unknown said...

Can you clarify which method is faster?

Anonymous said...

thank you a lot sir, this helps

Unknown said...

Thanks a lot for the explanation..

Sandeep Kamra said...

I understand all of your example but I can't understand one thing that where to use FETCH_OBJ

karunakar2093 said...

thanks for sharing this articles php-interview-questions-and-answers

Anonymous said...

Thank you!

Anonymous said...

i completed my 12th now, can i fill RRB Allahabad recruitment exam 2017.

Anonymous said...

top free ads posting list

Anonymous said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Oracle training in pune
Oracle Online Training
Oracle training in Bangalore
Oracle training in Sholingaanallur
Oracle training in marathahalli

sathish said...

Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

lavanya said...

A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
Java training in Chennai

Java Online training in Chennai

Java Course in Chennai

Best JAVA Training Institutes in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Training

Java Online Training

laxmi said...

VM Ware online training
Power shell online training
SCCM online training