Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
scriptingclass-phonebook/person.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 lines (34 sloc)
1.25 KB
This file contains 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 | |
require_once 'database.php'; | |
class Person { | |
protected $id; | |
protected $firstname; | |
protected $lastname; | |
protected $email; | |
protected $phone; | |
public function __construct($id=0) { | |
if ($id !== 0) { | |
$db = getDatabase(); | |
$query = $db->prepare( "SELECT * from people WHERE id = :id " ); // return all fields (*) from the table WHERE the id is the var $id passed to the function | |
$query->bindParam( ':id', $id ); | |
$query->execute(); // Run the SQL query on our database | |
$query->setFetchMode( PDO::FETCH_OBJ ); // A PHP method to tell the database we want to return all the data as objects with each table represented as a property of that object. This will be the case for most of our calls that return information from the database. http://php.net/manual/en/pdo.constants.php | |
$data = $query->fetchAll(); | |
$data = $data[0]; | |
// Build our local object | |
$this->id = $data->id; | |
$this->firstname = $data->firstname; | |
$this->lastname = $data->lastname; | |
$this->phone = $data->phone; | |
$this->email = $data->email; | |
} | |
} | |
public function get () { | |
$res['id'] = $this->id; | |
$res['firstname'] = $this->firstname; | |
$res['lastnamename'] = $this->lastname; | |
$res['email'] = $this->email; | |
$res['phone'] = $this->phone; | |
return $res; | |
} | |
} |