Last active
January 1, 2018 08:53
-
-
Save ta-riq/b35b2d9b3e493f192ec3d2e1650a64ee to your computer and use it in GitHub Desktop.
Doing Query from => DATABASE "mylife" TABLE "timeline" by PDO & practicing PHP class & method
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 | |
$config = require 'Config.php'; | |
require 'Connection.php'; | |
require 'QueryBuilder.php'; | |
return new QueryBuilder( Connection::Db_Connection( $config['Database'] ) ); |
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 | |
return [ | |
'Database' => [ | |
'App_Host' => 'mysql:host=localhost', | |
'Db_Name' => 'mylife', | |
'User_Name' => 'root', | |
'Password' => '', | |
'options' => [] | |
] | |
]; |
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 | |
class Connection | |
{ | |
public static function DB_Connection($config) | |
{ | |
try{ | |
return new PDO( | |
$config['App_Host'] . ';dbname=' . $config['Db_Name'], | |
$config['User_Name'], | |
$config['Password'], | |
$config['options'] | |
); | |
} | |
catch( PDOException $e ) | |
{ | |
die( $e->getMessage() ); | |
} | |
} | |
} |
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 | |
$QB = require 'bootstrap.php'; | |
// Need to create an Object of queryBuilder Class first............. | |
$tasks = $QB->selectAll( 'timeline' ); | |
require 'index.view.php'; |
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" | |
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<ul> | |
<?php foreach ( $tasks as $task ): ?> | |
<li> | |
<?php if ($task->completed): ?> | |
<strike> <?= $task->description; ?> </strike> | |
<?php else: ?> | |
<?= $task->description; ?> | |
<?php endif; ?> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</body> | |
</html> |
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 | |
class QueryBuilder | |
{ | |
protected $pdo; | |
public function __construct( $pdo ) | |
{ | |
return $this->pdo = $pdo; | |
} | |
public function selectAll( $table ) | |
{ | |
$statement = $this->pdo->prepare( "SELECT * FROM timeline" ); | |
$statement->execute(); | |
return $statement->fetchAll( PDO::FETCH_CLASS ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment