Skip to content

Instantly share code, notes, and snippets.

@ta-riq
Last active January 1, 2018 08:53
Show Gist options
  • Save ta-riq/b35b2d9b3e493f192ec3d2e1650a64ee to your computer and use it in GitHub Desktop.
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
<?php
$config = require 'Config.php';
require 'Connection.php';
require 'QueryBuilder.php';
return new QueryBuilder( Connection::Db_Connection( $config['Database'] ) );
<?php
return [
'Database' => [
'App_Host' => 'mysql:host=localhost',
'Db_Name' => 'mylife',
'User_Name' => 'root',
'Password' => '',
'options' => []
]
];
<?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() );
}
}
}
<?php
$QB = require 'bootstrap.php';
// Need to create an Object of queryBuilder Class first.............
$tasks = $QB->selectAll( 'timeline' );
require 'index.view.php';
<!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>
<?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