function dinnerBase_getOrderTicket($base_connect, $users, $expiredtime){
try {
extract($base_connect);
$dinnerBase = new PDO($host, $adm_user, $adm_password);
$dinnerBase -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dinnerBase->query("SET NAMES utf8");
$dinnerBase->query("SET CHARACTER SET utf8");
$query = "CREATE TABLE IF NOT EXISTS `:table_name` (
...
$stmt = $dinnerBase -> prepare($query);
$stmt -> execute(array(':table_name' => 'order_ticket'));
}
$dinnerBase = NULL;
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
а как быть с конструкцией try-catch?
class DB
{
protected static $instance = null;
final private function __construct() {}
final private function __clone() {}
public static function instance()
{
if (self::$instance === null)
{
$dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHARSET;
self::$instance = new PDO($dsn, DB_USER, DB_PASS);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->query("SET NAMES ".DB_CHARSET);
}
return self::$instance;
}
public static function __callStatic($method, $args) {
return call_user_func_array(array(self::instance(), $method), $args);
}
}
$stmt = DB::prepare($query);
function pdo()
{
static $pdo;
if (!$pdo) {
// в первый раз инициализируем объект PDO
}
return $pdo;
}
$stmt = pdo()->prepare('UPDATE order_tickets SET status = :stid WHERE id = :id');
...
//---------------- Процедура подключения к базе данных -----------------------//
function dbconnect() {
global $database;
if (!isset($database)) {
// Соединение по IP самое быстрое
// Сразу задаём кодировку соединения
$database = new PDO('mysql:host=127.0.0.1;dbname=base_name', "Vita", "god", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
// Устанавливаем уровень показа ошибок базы
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return true;
}
//----------------------------------------------------------------------------//
//--------------------- Функция выполнения запросов --------------------------//
function sql_query($query) {
global $errors;
global $database;
try {
if (dbconnect()) $return = $database->query($query);
}
catch (PDOException $e) {
$error = $e->getMessage();
writelog('sql_error', date("y.m.d H:m:s")."\t".$error);
$errors .= $error;
return false;
}
return $return;
}
//----------------------------------------------------------------------------//
//------------------------ Процедура записи в лог фаил -----------------------//
function writelog($typelog, $log_text) {
file_put_contents('logs/'.$typelog.'.txt', "$log_text\r\n", FILE_APPEND | LOCK_EX);
}
//----------------------------------------------------------------------------//
<?php
class DB {
protected static $_instance = null;
public $_pdo = null;
private function __construct() {
try {
$this -> _pdo = new PDO(
'mysql:host=localhost;dbname=xxx', 'root', 'xxx');
$this -> _pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this -> _pdo -> query("SET NAMES utf8");
$this -> _pdo -> query("SET CHARACTER SET utf8");
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
public static function getInstance() {
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
private function __clone() {
}
private function __wakeup() {
}
}
?>
$order_ticket_query = "CREATE TABLE IF NOT EXISTS `:table_name` (
`ID` INT AUTO_INCREMENT NOT NULL,
`oID` varchar(10),
`week` INT,
`name` varchar(70),
`expired` DATETIME,
`created` DATETIME,
`use` BOOL,
PRIMARY KEY (`ID`)
) CHARACTER SET utf8 COLLATE utf8_general_ci;";
$stmt = DB::getInstance() -> _pdo -> prepare($order_ticket_query);
$stmt -> execute(array(':table_name' => 'order_ticket'));