Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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