PDA

Orijinalini görmek için tıklayınız : PHP+Mysql Veri Tabanı Bağlantı Classı


heroman
26-12-2007, 17:53 PM
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9"/>
</head>

<body>
<?php
class Veritabani
{
private $host;
private $user;
private $psw;
private $db;
private $baglanti;
//Bağlantının Yapılacağı Ayarlar Yapılıyor...
public function __construct($host,$user,$psw,$db)
{
$this->host=$host;
$this->user=$user;
$this->psw=$psw;
$this->db=$db;
}

//Bağlantı VeriTabanıyla Sağlanıyor...
public function baglan()
{
try
{
$this->baglanti=@mysql_connect($this->host,$this->user,$this->psw);
if(!$this->baglanti)
{
throw new Exception("MySql veriatabanına bağlanamadım");
}
}
catch(exception $e)
{
echo $e->getmessage();
}
}
//veri tabanı dosyasından veritabanı seçiliyor...
public function sec()
{
try
{
if(!mysql_select_db($this->db))
throw new exception("Veri Tabanını Seçemedim");

}
catch(exception $e)
{
echo $e->getmessage();
}
}
//Veri Tabanı içindeki Sorgu Oluşturuluyor...
public function sorgu($sql)
{
try
{
$this->result=@mysql_query($sql);

if(!$this->result)
throw new exception("Sorgu Çalışmadı");

}
catch(exception $e)
{
echo $e->getmessage();
}

}
public function _destruct()
{
@mysql_close($this->baglanti);
}
}

?>
</body>
</html>
Daha Sonrada bağlantı şu şekilde yapılacak
Kod:

<?
$nesne=new Veritabani($host,$user,$psw,$dbase);
$nesne->baglan();
$nesne->sec();
?>