Created
May 16, 2018 16:23
-
-
Save rob1121/7ad2f7616040eb760996648df3a3c42f to your computer and use it in GitHub Desktop.
Fetching data using PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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