@huseyindundar; şu yazdığım sınıf işini görebilir. Daha iyisi için Doctrine veya ezSQL kullanabilirsin.
<?php
class MySQL
{
protected $dsn;
protected $host;
protected $username;
protected $password;
protected $database;
public $link;
public function __construct($host = '', $username = '', $password = '', $database = '')
{
if ( ! empty($host))
{
$this->setHost($host);
}
if ( ! empty($username))
{
$this->setUsername($username);
}
if ( ! empty($password))
{
$this->setPassword($password);
}
if ( ! empty($database))
{
$this->setDatabase($database);
}
}
public function getConnectionString()
{
return $this->dsn;
}
public function setConnectionString($dsn)
{
$this->dsn = $dsn;
return $this;
}
public function getHost()
{
return $this->host;
}
public function setHost($host)
{
$this->host = $host;
return $this;
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
return $this;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
return $this;
}
public function getDatabase()
{
return $this->password;
}
public function setDatabase($database)
{
$this->database = $database;
return $this;
}
public function getAttribute($attribute)
{
if ( ! is_null($this->link))
{
return $this->link->getAttribute($attribute);
}
return false;
}
public function setAttribute($attribute, $value)
{
if ( ! is_null($this->link))
{
$this->link->setAttribute($attribute, $value);
return $this;
}
return false;
}
public function connect()
{
if ($this->link instanceof PDO)
{
return $this;
}
if (empty($host) || empty($username) || empty($database))
{
return false;
}
$this->setConnectionString(sprintf('mysql:host=%s;dbname=%s', $this->getHost(), $this->getDatabase()));
try
{
$this->link = new PDO($this->getConnectionString(), $this->getUsername(), $this->getPassword());
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
throw new RuntimeException($e->getMessage());
}
return $this;
}
}