PHP Fetch PDO in different methods and practical examples

php fetch pdo method

In this tutorial, you will learn PHP Fetch PDO in different method and practical examples and use the PDOStatement object to fetch data from the database and php pdo examples.  
 

Fetch() is a method of PDOStatement class. The PHP fetch() method allows you to retrieve a row from the result set associated with the PDOStatement object.



The fetch() method fetches a single row from a database result set and moves the information to the next row in the result set. So, the next call to the fetch() method returns the next row from the result set.

To get all the rows from a data result, you usually use the fetch() method in a while loop. We usually create a table or design on a website by fetching results from a database.




$ads={1}

 

Fetch()  methods the complete list can only be found on the main page of the PHP Manual with all PDO constants, where they are all just a few symbols and explanations of terms.

Here, we are discussing only a few fetch methods that accept the parameter.

The most commonly used different types of php fetch method in pdo modes are:

  • PDO::FETCH_BOTH - It returns a form of an array indexed by both numeric and associative indexes (column name).
  • PDO::FETCH_NUM -  It returns a numeric indices only.
  • PDO::FETCH_ASSOC - It returns an indexed by column name.
  • PDO::FETCH_CLASS - It returns a new class instance by mapping .
  • PDO::FETCH_OBJ - It returns an object without a class name. And the returned the result set with corresponding column names


Using the PHP fetch() method

In the PHP fetch() method doesn't accept the parameter in the query, you can fetch a row from the result and follow the steps:

First, we need to PHP PDO connection database using the script "dbconfig.php" and call the query to execute the method using of db object.
 

If you don't know how to create a PHP PDO connection click here

And then, the query execute and fetch() each row from the result and creates a "while loop" to php fetch the row. 


PDO::FETCH_ASSOC




<?php

require 'dbconfig.php';

$sql = 'SELECT student_name, dept_name FROM student';
$stmt = $db->query($sql);
echo "<table border='1'>
<tr>
<th>student name</th>
<th>department name</th>
</tr>";

while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {

	echo '<tr>';
    echo '<td>' .$row['student_name'] . '</td>';
	echo '<td>' .$row['dept_name']. '</td>';
	echo '</tr>';
	
}
echo "</table>";

?>


php fetch pdo method


PDO::FETCH_BOTH



<?php

require 'dbconfig.php';

$stmt = $db->query("SELECT * from student");
$row = $stmt->fetch(PDO::FETCH_BOTH);
print_r($row);

?>


output:


Array ( [id] => 1001 [0] => 1001 [student_name] => chandrakumar [1] => 
chandrakumar [dept_name] => cse [2] => cse [status] => 1 [3] => 1 ) 



PDO::FETCH_NUM



<?php

require 'dbconfig.php';

$stmt = $db->query("SELECT * from student");
$row = $stmt->fetch(PDO::FETCH_NUM);
print_r($row);

?>


output:



Array ( [0] => 1001 [1] => chandrakumar [2] => cse [3] => 1 ) 


PDO::FETCH_OBJ


In the "fetch_obj" return set without class name provided. It should return set before the stdClass instance.


<?php

require 'dbconfig.php';

$stmt = $db->query("SELECT * from student");
$row = $stmt->fetch(PDO::FETCH_OBJ);
print_r($row);

?>


output:



stdClass Object ( [id] => 1001 [student_name] => 
chandrakumar [dept_name] => cse [status] => 1 ) 


PDO::FETCH_CLASS


In the pdo fetch class are some manipulation in pdo. we are create a instance of a class with a given class name for mapping returned columns to the class properties.
 
Now, we are get the single row. However, although you could use the familiar fetch() method shown below:



<?php
class student
{
    private $student_name;
	private $dept_name;

    public function getdetails()
    {
        echo $this->student_name .'<br/>';
        echo $this->dept_name;
    }
}

$stmt = $db->query("SELECT * FROM student");
$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');
$student = $stmt->fetch();
$student->getdetails();
?>


output:


chandrakumar
cse
Previous Post Next Post