<?php
//
define('TABLE_USER', 'my_users');
//
$pdoConnection = null;
//
function getPdoConnection()
{
global $pdoConnection;
$dsn = '
mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$pdoConnection = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
//
function getAllUsers()
{
$
sql = 'SELECT * FROM :table';
$connection = getPdoConnection();
$
query = $connection->prepare($
sql);
$
query->bindParam(':table', TABLE_USER, PDO::PARAM_STR);
$
query->execute();
if ($
query === false) {
return false;
} else {
return $
query;
}
}
function gerUser($username = '')
{
$
sql = 'SELECT * FROM :table WHERE username = :username';
$connection = getPdoConnection();
$
query = $connection->prepare($
sql);
$
query->bindParam(':table', TABLE_USER, PDO::PARAM_STR);
$
query->bindParam(':username', $username, PDO::PARAM_STR);
$
query->execute();
if ($
query === false) {
return false;
} else {
return $
query;
}
}