Wednesday 11 July 2018

PHP PDO: Possible Fetch Mode

PHP PDO: Possible Fetch Mode

Now, 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

$dbhost = "localhost";

$dbname= "pdo";

$dbuser= "root";

$dbpass= "";

// 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);

echo "<br>";

}

// 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

<?php

// configuration

$dbhost = "localhost";

$dbname= "pdo";

$dbuser= "root";

$dbpass= "";

// 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);

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

// fetch

while($r = $q->fetch()){

print_r($r);

echo "<br>";

}

// 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())
<?php

// configuration

$dbhost     = "localhost";

$dbname           = "pdo";

$dbuser           = "root";

$dbpass           = "";

// 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);

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

// fetch

while($r = $q->fetch()){

print_r($r);

echo "<br>";

}

// 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)
<?php

// configuration

$dbhost     = "localhost";

$dbname           = "pdo";

$dbuser           = "root";

$dbpass           = "";

// 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);

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

$q    = $conn->query($sql);

// fetch

while($r = $q->fetch()){

print_r($r);

echo"<br>";

}

// 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 )

?>

0 comments:

Post a Comment