WE often need to collect one record from the table to display or to use in our script. To select one record we will use Pdo -> fetch to get next row from a result set. Here are some examples on how to use fetch on different conditions.
We kept our Pdo database connection details in config.php file and called it here.
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
require "config.php"; // Database connection details.
//////// End of Database connection /////////
$id='3'; // Collecting one record with id=3
$count=$dbo->prepare("select * from pdo_admin where id=:id");
echo " Success <br>";
$row = $count->fetch(PDO::FETCH_OBJ);
echo "<hr><br>Admin = $row->userid";
echo "<br> pw =$row->password<br>";
We have used print_r function to display the array WE have used FETCH_OBJ parameter get property name corresponding to column name. Same way we will try other parameters
In your downloaded script ( file name : pdo-fetch.php ) the area to change the part of the code is marked. You can replace them with these lines to check the output
We have to use column number starting from 0 in place of column name , omitted other lines and listed here the changes in above code.