020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO::FETCH_COLUMN
[MySQL PDO Const]

PDO::FETCH_COLUMN (integer)

Specifies that the fetch method shall return only a single requested column from the next row in the result set.

Used with:

  • PDOStatement:: fetchAll

 

PDO::FETCH_COLUMN can specify, which column to fetch. Indexing columns start from 0. 0 index is set for first column from left.

$sth->fetchAll(PDO::FETCH_COLUMN,1);

PDO::FETCH_COLUMN allows to group results by selected column from result set. PDO::FETCH_GROUP is separated by bitwise-OR, also called as pipe "|"

$sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP,2);

 

 

PDO::FETCH_COLUMN with no column selected. By default, first column will be selected (column with index 0).

code:

//FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') formats date properly - according to MySQL storage standard for datetime
//one could use php to format data: $entry_date = time("Y-m-d H:i:s", 1329109080);
$sql = "SELECT entry_title, entry_body, status FROM posts WHERE entry_date < FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') ";
$sth = $dbh->prepare($sql);

$entry_date = '1329109080';
$sth->bindParam(':entry_date', $entry_date, PDO::PARAM_STR);
$sth->execute();
$sth->bindColumn('entry_title', $entry_title, PDO::PARAM_STR);
$result = $sth->fetchAll(PDO::FETCH_COLUMN);

print_r($result);
var_dump($result);

output:

print_r:

Array
(
    [0] => post 1
    [1] => post 2
    [2] => post 3
    [3] => post 4
)
 

var_dump:

array
  0 => string 'post 1' (length=6)
  1 => string 'post 2' (length=6)
  2 => string 'post 3' (length=6)
  3 => string 'post 4' (length=6)
  4 => string 'post 5' (length=6)
 

 

PDO::FETCH_COLUMN with column #1 selected. Index #1 meanes 2-nd column from left.

code:

//FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') formats date properly - according to MySQL storage standard for datetime
//one could use php to format data: $entry_date = time("Y-m-d H:i:s", 1329109080);
$sql = "SELECT entry_title, entry_body, status FROM posts WHERE entry_date < FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') ";
$sth = $dbh->prepare($sql);

$entry_date = '1329109080';
$sth->bindParam(':entry_date', $entry_date, PDO::PARAM_STR);
$sth->execute();
$sth->bindColumn('entry_title', $entry_title, PDO::PARAM_STR);
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 1);

print_r($result);
var_dump($result);

 

output:

print_r:

 

Array
(
    [0] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [1] => 2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [2] => 3: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
)

var_dump:

 

array
      0 => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      1 => string '2: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
      2 => string '3: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)

 

 

 

 

PDO::FETCH_COLUMN with grouping results by selected column. Grouping is done by PDO::FETCH_GROUP.
Items are groupped by status, which comes as: 0, 1, 2

code:

//FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') formats date properly - according to MySQL storage standard for datetime
//one could use php to format data: $entry_date = time("Y-m-d H:i:s", 1329109080);
$sql = "SELECT entry_title, entry_body, status FROM posts WHERE entry_date < FROM_UNIXTIME(:entry_date, '%Y-%m-%d %H:%i:%s') ";
$sth = $dbh->prepare($sql);

$entry_date = '1329109080';
$sth->bindParam(':entry_date', $entry_date, PDO::PARAM_STR);
$sth->execute();
$sth->bindColumn('entry_title', $entry_title, PDO::PARAM_STR);
$result = $sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP,2);

print_r($result);
var_dump($result);

 

 

output:

print_r:

 

Array
(
    [0] => Array
        (
            [0] => post 1
            [1] => post 2
            [2] => post 3
        )
 
    [2] => Array
        (
            [0] => post 4
            [1] => post 5
            [2] => post 6
        )
 
    [1] => Array
        (
            [0] => post 7
            [1] => post 8
            [2] => post 9
        )
)

var_dump:

 

array
  0 => 
    array
      0 => string 'post 1' (length=6)
      1 => string 'post 2' (length=6)
      2 => string 'post 3' (length=6)
  2 => 
    array
      0 => string 'post 4' (length=7)
      1 => string 'post 5' (length=7)
      2 => string 'post 6' (length=7)
  1 => 
    array
      0 => string 'post 7' (length=7)
      1 => string 'post 8' (length=7)
      2 => string 'post 9' (length=7)