How to Fetch a Single Row of Data from a Database using PHP PDO

Fetch single row with PHP PDO

$st = $pdo->prepare('SELECT * FROM test LIMIT 1');
$st->execute();
$row = $st->fetch(PDO::FETCH_ASSOC);
  • $pdo->prepare - prepare given query to execute
  • $st->execute( - run query on the server
  • fetch - returns single row from resulting set
  • FETCH_ASSOC - will return row as an associative array (rather than numbered array)

group: fetch

Example:

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'usr', 'pwd');

$st = $pdo->prepare('SELECT * FROM test LIMIT 1');
$st->execute();
$row = $st->fetch(PDO::FETCH_ASSOC);
print_r($row);
Array
(
    [id] => 1
    [age] => 25
    [name] => A
    [data] => 
)

Additional keywords

  • get
  • retrieve
  • query

View on GitHub: https://github.com/Onelinerhub/onelinerhub/blob/main/php-pdo/fetch-single-row-with-php-pdo.md 

#php 

How to Fetch a Single Row of Data from a Database using PHP PDO
12.70 GEEK