Skip to content

Instantly share code, notes, and snippets.

@rob1121
Created May 16, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob1121/7ad2f7616040eb760996648df3a3c42f to your computer and use it in GitHub Desktop.
Save rob1121/7ad2f7616040eb760996648df3a3c42f to your computer and use it in GitHub Desktop.
Fetching data using PDO
<?php
//connect to database
$query = $db->query('SELECT * FROM tbl_name');
$row = $query->fetch(); //get first row
//itirate to all data
$row = $query->fetchAll(PDO::FETCH_OBJ);
<?php
//connect to database
class GuestBookentry {
public $id,
$name,
$message,
$posted,
$entry // custom column that are not really in database table column
;
public function __construct() {
$this->entry = "{$this->name} posted: {$this->message}";
}
}
$query = $db->query('SELECT * FROM tbl_name');
$query->setFetchMode(PDO::FETCH_CLASS, 'GuestBookentry');
while($row = $query->fetch()) {
echo $row->entry;
}
<?php
//connect to database
$query = $db->query('SELECT * FROM tbl_name');
$row = $query->fetch(); //get first row
//itirate to all data
while($row = $query->fetch(PDO::FETCH_OBJ)) {
//do action to $row
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment