soulmy adlı üyeden alıntı:
mesajı görüntüle
inceliyorum sağol
--R10.NET; Flood Engellendi -->-> Yeni yazılan mesaj 19:08:50 -->-> Daha önceki mesaj 18:45:55 --
bunu pdo şeklinde kodlanmıs atabilir misiniz
19
●39.611
<?php
$baglanti = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("uyeler",$baglanti) or die(mysql_error());
mysql_query("SET CHARACTER SET utf-8");
mysql_query("SET NAMES utf-8");
?>index.php dosyamız : <?php error_reporting(0); require "baglan.php"; echo "Bağlantı başarılı" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> </head> <body> </body> </html>index.php yer alan kod yani error_reporting(0); ile başarılı bir şekilde bağlantınızı sağlayabilirsiniz.
<?php
$database = array(
'host' => '127.0.0.1',
'name' => 'test',
'user' => 'root',
'pass' => '',
);
$connectionString = 'mysql:host=' . $database['host'] . ';dbname=' . $database['name'] . ';charset=UTF-8';
try
{
$connection = new PDO($connectionString, $database['user'], $database['pass']);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $exception)
{
die($exception->getMessage());
}
/**
* Örnek MySQL sorgusu
**/
/**
* Üye numarası 1 olan kullanıcının üye numarası, adı ve soyadını ekrana yazdıralım.
**/
$ID = 1;
try
{
$statement = $connection->prepare('SELECT `ID`, `name`, `surname` FROM `users` WHERE `ID` = :ID');
$statement->bindParam('ID', $ID, PDO::PARAM_INT);
$statement->execute();
$user = $statement->fetch(PDO::FETCH_ASSOC);
print_r($user);
/**
* Çıktı:
*
* Array
* (
* [ID] => 1
* [name] => Ogün
* [surname] => Karakuş
* )
**/
}
catch (PDOException $exception)
{
die($exception->getMessage());
}
<?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;
}
}