Solution Review: PHP Database
Let's look at the solution of the PHP Database challenge.
We'll cover the following...
We'll cover the following...
Solution
<?php
// DB credentials.
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'educative');//database username
define('DB_PASS', 'root');// database passord
define('DB_NAME', 'testing'); //database name
class Db {
// Hold the handle for the PDO object in a private variable.
private $dbh;
// Establish database connection
// or display an error message.
function __construct()
{
try {
$this->dbh = new \PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
echo "connected\n";
}
catch (PDOException $e) {
exit("Error: " . $e->getMessage());
}
}
// Method to get the database handler
// so it can be used outside of this class.
function get()
{
return $this->dbh;
}
// Set the PDO object to null to close the connection.
function close(){
$this->dbh = null;
}
}
class User {
private $tableName = 'users';
private $dbCon;
// First, save the connection in a private property.
function __construct($dbCon) {
$this->dbCon = $dbCon;
// Creating table "users" in database
$sql = "CREATE TABLE IF NOT EXISTS users (
id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name varchar(60) DEFAULT NULL,
phone varchar(14) DEFAULT NULL,
city varchar(60) DEFAULT NULL,
date_added date DEFAULT NULL,
PRIMARY KEY (id)
)";
// use exec() because no results are returned
$this->dbCon->exec($sql);
echo "Table users created successfully\n";
}
// Insert new user to the database.
function insert($name,$phone,$city) {
// The insert query.
$sql = "INSERT INTO `{$this->tableName}` (`name`,`phone`, `city`, `date_added`)
VALUES
(:name,:phone,:city,:created)";
// Bind and filter.
$query = $this->dbCon->prepare($sql);
$query->bindParam(':name',$name,PDO::PARAM_STR);
$query->bindParam(':phone',$phone,PDO::PARAM_STR);
$query->bindParam(':city',$city,PDO::PARAM_STR);
$now = date('Y-m-d');
$query->bindParam(':created',$now,PDO::PARAM_STR);
$query -> execute();
// The id of the newly created row in the table.
$lastInsertId = $this->dbCon->lastInsertId();
if($lastInsertId>0)
return $lastInsertId;
else
return false;
}
function getUserById($id) {
$sql = "SELECT * FROM `{$this->tableName}` WHERE `id` = :id LIMIT 1";
$query = $this->dbCon->prepare($sql);
$query -> bindParam(':id', $id, PDO::PARAM_INT);
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query -> rowCount() < 1)
return false;
return $results[0];
}
// Get all the users.
function getAll() {
$sql = "SELECT * FROM `{$this->tableName}` WHERE 1";
$query = $this->dbCon->prepare($sql);
$query -> bindParam(':id', $id, PDO::PARAM_INT);
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query -> rowCount() < 1)
return false;
return $results;
}
function updateUser($id,$array) {
$sql = "UPDATE `{$this->tableName}` SET ";
$columns = array();
foreach($array as $fieldName => $value) {
$columns[] = "`{$fieldName}` = :{$fieldName}";
}
$sql .= implode(',',$columns);
$sql .= ' WHERE `id` = :id';
$query = $this->dbCon->prepare($sql);
foreach($array as $fieldName => $value) {
// Use bindValue, not bindParam because
// bindParam only gets its value at the time of execution.
$query -> bindValue(":{$fieldName}",$value);
}
$query -> bindParam(':id', $id, PDO::PARAM_INT);
$query -> execute();
if($query -> rowCount() < 1)
return false;
return $id;
}
function delete($id) {
$sql = "DELETE FROM `{$this->tableName}` WHERE `id`=:id";
$query = $this->dbCon->prepare($sql);
$query -> bindParam(':id', $id, PDO::PARAM_INT);
$query -> execute();
if($query -> rowCount() < 1)
return false;
return true;
}
}
// Create the pdo object.
$db = new Db;
// Get the connection.
$dbCon = $db->get();
// When creating the new User class
// pass the connection.
$userObj = new User($dbCon);
// Test insertion.
echo "Inserting user in table\n";
var_dump($userObj->insert('James Tiberius Kirk','0544308209','Riverside, Iowa'));
// Test selection.
echo "\nGetting user by id\n";
var_dump($userObj->getUserById(1));
echo "\nGetting all users the table\n";
var_dump($userObj->getAll());
// Test update.
echo "\nUpdated the user\n";
$array=['name'=>'Captain Kirk','city'=>'Tarsus IV','date_added'=>'2233-03-22'];
var_dump($userObj->updateUser(1,$array));
var_dump($userObj->getUserById(1));
// Test delete.
echo "\nDeleted the user\n";
var_dump($userObj->delete(1));
Solution
Explanation
-
Lines 8–11: We define database credentials.
-
Lines 13–29: We write the
Dbclass with the constructor method, which will establish the database connection. -
Line 33: We add the
get()method to get the database connection to use it outside the class. -
Line 39: We ...