PHP MySQL PDO Class Load Record
From w3cyberlearnings
Contents |
PHP PDO Uses Class to get record
This example uses the PHP class to request record specifies by the id. You can use this concept to build a more advance class framework for your project.
Example 1
<?php class user { public $id, $first_name, $last_name, $email; public function display() { echo 'User Id: '. $this->id . '<br/>'; echo 'First Name: '. $this->first_name . '<br/>'; echo 'Last Name: '. $this->last_name. '<br/>'; echo 'Email: '. $this->email.'<br/>'; echo '<br/>'; } public function LoadFromId($id) { $db = new PDO('mysql:host=localhost;dbname=w3cyberlearning','user2000','password2000'); $sq = $db->query("SELECT id, first_name, last_name, email FROM user_infor WHERE id={$id}"); $row = $sq->fetchObject(__CLASS__) ; $this->id= $row->id; $this->last_name = $row->last_name; $this->email = $row->email; $this->first_name = $row->first_name; } } $test_user = new user(); $test_user->LoadFromId(1); $test_user->display(); ?>
Output
User Id: 1 First Name: bob Last Name: lema Email: [email protected]