phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PDO: Posibble Fetch Mode

Tag: PHP Data Objects, PDO, extension, PHP extension, fetch data, fetch mode   Category: PHP Database
post: 19 Apr 2008 read: 927


PHP Data Objects (PDO) Step By Step Tutorial - Part 5: In this post, we will see posible mode to fetch data. In mysql, we know there are mysql_fetch_row(), mysql_fetch_array(), and mysql_fetch_assoc(). How about in PDO?

It is general that we use, without set fetch mode:


<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$sql = "SELECT title FROM books ORDER BY title";
$q	 = $conn->query($sql);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result
//Array ( [title] => PHP AJAX [0] => PHP AJAX ) 
//Array ( [title] => PHP API [0] => PHP API ) 
//Array ( [title] => PHP Eclipse [0] => PHP Eclipse ) 
//Array ( [title] => PHP Prado [0] => PHP Prado ) 
//Array ( [title] => PHP SEO [0] => PHP SEO ) 
//Array ( [title] => PHP Web Services [0] => PHP Web Services ) 
//Array ( [title] => PHP Zend Framework [0] => PHP Zend Framework )
?>

Fech Association


// query
$sql = "SELECT title FROM books ORDER BY title";
$q	 = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result
//Array ( [title] => PHP AJAX) 
//Array ( [title] => PHP API) 
//Array ( [title] => PHP Eclipse) 
//Array ( [title] => PHP Prado) 
//Array ( [title] => PHP SEO) 
//Array ( [title] => PHP Web Services) 
//Array ( [title] => PHP Zend Framework)

Fetch Num (like mysql_fetch_row())


$q->setFetchMode(PDO::FETCH_NUM);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result
//Array ( [0] => PHP AJAX ) 
//Array ( [0] => PHP API ) 
//Array ( [0] => PHP Eclipse ) 
//Array ( [0] => PHP Prado ) 
//Array ( [0] => PHP SEO ) 
//Array ( [0] => PHP Web Services ) 
//Array ( [0] => PHP Zend Framework )

Fetch Both (default)


$q->setFetchMode(PDO::FETCH_BOTH);
$q	 = $conn->query($sql);

// fetch
while($r = $q->fetch()){
  print_r($r);
}

// result
//Array ( [title] => PHP AJAX [0] => PHP AJAX ) 
//Array ( [title] => PHP API [0] => PHP API ) 
//Array ( [title] => PHP Eclipse [0] => PHP Eclipse ) 
//Array ( [title] => PHP Prado [0] => PHP Prado ) 
//Array ( [title] => PHP SEO [0] => PHP SEO ) 
//Array ( [title] => PHP Web Services [0] => PHP Web Services ) 
//Array ( [title] => PHP Zend Framework [0] => PHP Zend Framework )
?>




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting