Witam,
Czy mógłby ktoś przejrzeć kod i wytknąć mi moje błędy?
<?php
/**
* TibiaServ
*
* @author Juliusz Marciniak <juliusz.marciniak@gmail.com>
* @date 20.10.2009
* @version 0.1
* @copyright Copyright (c) 2009 Juliusz Marciniak, All Rights Reserved
*/
class Pdo_Database_Engine
{
/**
* Connection to the database.
*
* @access public
* @param string The database DSN.
* @param string The database username. (depends on DSN)
* @param string The database user's password. (depends on DSN)
* @param array The databases driver options (optional)
* @return boolean True on success
*/
public function __construct
($db_dsn, $db_login = '', $db_password = '', $db_driver_options = array()) {
try
{
$this -> pdo = new PDO($db_dsn, $db_login, $db_password, $db_driver_options);
}
catch(PDOException $exception)
{
die('Connection failed: <br /><b>' . $exception -> getMessage() . '</b>'); }
$this -> pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
/**
* Performs a simple select query.
*
* @access public
* @param string The table name to be queried.
* @param string List of fields to be selected.
* @param string SQL formatted list of conditions to be matched.
* @param array List of options: bind, order by, limit and fetch_array
*/
public function query_select
($table, $fields = '*', $conditions = '', $options = array()) {
$query = 'SELECT ' . $fields . ' FROM ' . $table;
if ($conditions != '')
{
$query .= ' WHERE ' . $conditions;
}
if (isset($options['order_by'])) {
$query .= ' ORDER BY ' . $options['order_by'];
}
if (isset($options['limit'])) {
$query .= ' LIMIT ' . $options['limit'];
}
if (isset($options['bind'])) {
$bind = $options['bind'];
$query = $this -> pdo -> prepare($query);
foreach($bind as $parameter => $value)
{
{
$query -> bindValue($parameter, $value[0], $value[1]);
}
else
{
$query -> bindValue($parameter, $value);
}
}
$query -> execute();
}
else
{
$query = $this -> pdo -> query($query);
}
if (isset($options['fetch_array'])) {
{
$fetch_array = $options['fetch_array'];
return $this -> fetch_array($query, $fetch_array['fetch_style']);
}
else
{
return $this -> fetch_array($query);
}
}
else
{
return $query;
}
}
public function fetch_array($query, $fetch_style = PDO::FETCH_BOTH)
{
{
return '<b>Warning: Invalid argument</b>';
}
$fetch_array = $query -> fetch($fetch_style);
return $fetch_array;
}
/**
* The end of the connection to the database.
*
* @access public
* @return boolean True on success
*/
public function __destruct()
{
$this -> pdo = null;
return true;
}
}
?>
Później wykorzystuje to na przykład tak:
$a = new Pdo_Database_Engine
('mysql:dbname=test;host=localhost', 'xxx', 'xxx', array(PDO
::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$b = $a -> query_select('`bany`', '*', '`typ` = :typ AND `asdasd` = :asdasd', array('fetch_array' => array(true, 'fetch_style' => PDO
::FETCH_ASSOC), 'bind' => array(':typ' => array(2, PDO
::PARAM_INT), ':asdasd' => 2
)));
Początek mam dobrze, bo patrzyłem z manuala, chodzi mi o te 2 metody.
Ten post edytował julek12 20.11.2009, 08:47:48