Search
 
SCRIPT & CODE EXAMPLE
 

PHP

pdo fetch


<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Exercise PDOStatement::fetch styles */
print("PDO::FETCH_ASSOC: ");
print("Return next row as an array indexed by column name
");
$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("
");

print("PDO::FETCH_BOTH: ");
print("Return next row as an array indexed by both column name and number
");
$result = $sth->fetch(PDO::FETCH_BOTH);
print_r($result);
print("
");

print("PDO::FETCH_LAZY: ");
print("Return next row as an anonymous object with column names as properties
");
$result = $sth->fetch(PDO::FETCH_LAZY);
print_r($result);
print("
");

print("PDO::FETCH_OBJ: ");
print("Return next row as an anonymous object with column names as properties
");
$result = $sth->fetch(PDO::FETCH_OBJ);
print $result->name;
print("
");
?>

Comment

PDO::FETCH_OBJ


It should be mentioned that this method can set even non-public properties. It may sound strange but it can actually be very useful when creating an object based on mysql result.
Consider a User class:

<?php
class User {
   // Private properties
   private $id, $name;

   private function __construct () {}

   public static function load_by_id ($id) {
      $stmt = $pdo->prepare('SELECT id, name FROM users WHERE id=?');
      $stmt->execute([$id]);
      return $stmt->fetchObject(__CLASS__);
   }
   /* same method can be written with the "name" column/property */
}

$user = User::load_by_id(1);
var_dump($user);
?>

fetchObject() doesn't care about properties being public or not. It just passes the result to the object. Output is like:

object(User)#3 (2) {
  ["id":"User":private]=>
  string(1) "1"
  ["name":"User":private]=>
  string(10) "John Smith"
}
Comment

php pdo fetch from db

$pdo = new PDO('mysql:host=$host; dbname=$database;', $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM auction WHERE name = :name');
$stmt->bindParam(':name', $_GET['searchdivebay']);
$stmt->execute(array(':name' => $name);
Comment

PREVIOUS NEXT
Code Example
Php :: yii2 sql query 
Php :: phpspreadsheet setcellvalue row background color 
Php :: twig symfony get route 
Php :: php create 404 error 
Php :: foreach comma separated string php 
Php :: carbon parse from format 
Php :: yii2 advanced nginx 
Php :: laravel get last 5 records 
Php :: how to add properties to the request object 
Php :: wordpress echo the page title 
Php :: php regex remove characters from string 
Php :: how tdo you convert a stringto lowercase in php 
Php :: php check if extension is installed 
Php :: run php server mac 
Php :: php array_sum 
Php :: yii2 a href confirm 
Php :: Interval Between Different Dates 
Php :: laravel route fallback 
Php :: Extract images from a folder in php 
Php :: php format date 
Php :: disable edit-link storefront 
Php :: share link in twitter php 
Php :: stack once laravel 
Php :: how to get the values of other fields in acf validate values 
Php :: php datetime to mysql 
Php :: codeigniter 3 Configured database connection has cache enabled 
Php :: get parameter in php 
Php :: phpmyadmin import size limit 
Php :: kill laravel server 
Php :: laravel_8 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =