Basit MySQLi Bağlantı Sınıfı

<?php
class mSQLi {
	protected $MySQLiHost    = "localhost";
	protected $MySQLiPort    = "3306";
	protected $MySQLiUser    = "root";
	protected $MySQLiPasswd  = "";
	protected $MySQLiName    = "php";
	protected $MySQLiBaglanti;
	public function __construct(){
		return $this->MySQLiBaglanti();
	}
	public function MySQLiBaglanti(){
		$this->MySQLiBaglanti = mysqli_connect($this->MySQLiHost.":".$this->MySQLiPort, $this->MySQLiUser, $this->MySQLiPasswd,$this->MySQLiName) or die("Veritabanı Bağlantı Hatası: ".mysqli_errno($this->MySQLiBaglanti)." : ".mysqli_error($this->MySQLiBaglanti));
		return true;
	}
	public function MySQLiSonlandir(){
		mysqli_close($this->MySQLiBaglanti);
		return true;
	}
}
$db = new mSQLi;
$db->MySQLiBaglanti();
$db->MySQLiSonlandir();
?>

 

Basit Mysqli Veritabanı Erişim ve Tablolu Çıktı Sınıfı

<?php
	class mysql{
		var $host;
		var $kullanici;
		var $sifre;
		var $tablo;
		var $idbaglanti = 0;
		var $idsorgu = 0;
		function baglan($sunucu,$kullanici,$sifre,$tablo){
			$this->host = $sunucu;
			$this->kullanici = $kullanici;
			$this->sifre = $sifre;
			$this->tablo = $tablo;
			$this->idbaglanti = mysqli_connect($sunucu,$kullanici,$sifre,$this->tablo) or die("MySQL'e baglanılamadı: ".mysqli_error($this->idbaglanti));
			return $this->idbaglanti;
		}
		function sorgu($sorgu){
			$this->idsorgu = @mysqli_query($this->idbaglanti,$sorgu) or die("Sorgu Çalıştırılamadı");
			return $this->idsorgu;			
		}
		function sayi(){
			return mysqli_num_fields($this->idsorgu);
		}
		function alanadi($alan){
			return mysqli_fetch_field_direct($this->idsorgu,$alan)->name;
		}
		function cikti(){
			echo "<table border=1>\n";
			echo "<tr>\n";
			for ($i=0;$i<$this->sayi();$i++){
				echo "<th>".$this->alanadi($i)."</th>\n";
			}
			echo "</tr>\n";
			while ($satir = mysqli_fetch_row($this->idsorgu)) {
				echo "<tr>\n";
				for ($i=0;$i<$this->sayi();$i++){
					echo "<td>".$satir[$i]."</td>\n";
				}
				echo "</tr>\n";
			}
			echo "</table>\n";
		}
	}
	$m = new mysql();
	$m->baglan("localhost","root","","php");
	$m->sorgu("select id,sayfa,tamam from php limit 2");
	$m->cikti();
?>

Çıktısı

<table border=1>
    <tr>
        <th>id</th>
        <th>sayfa</th>
        <th>tamam</th>
    </tr>
    <tr>
        <td>1</td>
        <td>7</td>
        <td>1</td>
    </tr>
    <tr>
        <td>2</td>
        <td>3</td>
        <td>1</td>
    </tr>
</table>

 

MyDB ile Mysqli Veritabanı Sorgu ve Hata İzleme

<?php
class myDB{ 
	var $link;
	function __construct(){
		$dbserver = "localhost";
		$dbuser = "root"; 
		$dbpass = "";  
		$dbname = "php";	
		$this->link = mysqli_connect($dbserver,$dbuser,$dbpass,$dbname);
	}
  function sqli(){
		$sorgu = func_get_arg(0); 
		if (!$veri = mysqli_query($this->link,$sorgu)){
			if(func_num_args()>1){ 
				$aranan = basename(func_get_arg(1));             
			}else{ 
				$aranan = "<i>bilinmeyen</i>"; 
			} 
			if(func_num_args()>2){ 
				$satir = func_get_arg(2);
			}else{ 
				$satir = "<i>bilinmeyen</i>"; 
			} 
			trigger_error("<p style=\"color:#ff0000;\"><b>MYDB-HATA:<br />Sorgu</b> ".$sorgu." <b> hata döndürdü.</b><br />".mysqli_error($this->link)." <br /> ".$aranan." <b>dosyasında satır</b> ".$satir." </p>", E_USER_WARNING); 
		} 
		$dizi = explode(" ",$sorgu); 
		$dizi[0] = strtoupper($dizi[0]); 
		if ($dizi[0]=="SELECT"){ 
			$say = mysqli_num_rows($veri); 
		}else{ 
			$say = mysql_affected_rows(); 
		} 
		return array($veri, $say); 
	}
	function __destruct(){
		mysqli_close($this->link);
	}
}
$my = new myDB;	
list($veri, $say) = $my->sqli("SELECT id,sayfa FROM php limit 2",__FILE__,__LINE__);
foreach($veri as $a){
	print_r($a);
}
?>

Çıktısı

Array
(
    [id] => 1
    [sayfa] => 7
)
Array
(
    [id] => 2
    [sayfa] => 3
)

Hata testi

$my = new myDB;	
list($veri, $say) = $my->sqli("WHERE SELECT FROM php",__FILE__,__LINE__);
foreach($veri as $a){
	print_r($a);
}

Hata Çıktısı

Warning:
MYDB-HATA:
Sorgu WHERE SELECT FROM php hata döndürdü.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE SELECT FROM php' at line 1 
index.php dosyasında satır 40

in C:\www\index.php on line 24

Warning: Invalid argument supplied for foreach() in C:\www\index.php on line 41

 

Veritabanı Bağlantı Bilgilerini Korumak

<?php
class db {
	var $veritabaniadi;
	var $link;
	var $hatanumarasi;
	var $hata;
	function db() {
		$sunucu = "localhost";
		$veritabani = "php";
		$kullaniciadi = "root";
		$sifre = "";
		$this->veritabaniadi = $veritabani;
		$this->link = mysqli_connect($sunucu,$kullaniciadi,$sifre,$veritabani);
		$this->hatanumarasi = mysqli_errno($this->link);
		$this->hata = mysqli_error($this->link);

	}
	function veritabaniadi() {
		return $this->veritabaniadi;
	}
	function link(){
		return $this->link;
	}
	function hatanumarasi(){
		return $this->hatanumarasi;
	}
	function  hata() {
		return $this->hata;
	}
}
$db=new db();
$sorgu=mysqli_query($db->link(),"select * from php limit 1");
while($s=mysqli_fetch_array($sorgu)){
	echo $s["id"]."<br>";
	echo $db->hata."<br>";
	echo $db->hatanumarasi."<br>";
	echo $db->veritabaniadi;
}
?>

Çıktısı

1

0
php

 

cSQLi – Mysqli Veritabanı Sınıfı

<?php
header('Content-Type: text/html; charset=utf-8');
class cSQLi {
	public $oci;
	public $rs;
	public function __construct($server, $user, $pass, $db) {
		$this->oci = new mysqli($server, $user, $pass, $db);
		if (mysqli_connect_errno()) {
				printf("Bağlantı Hatası: %s\n", mysqli_connect_error());
   			exit();
		}
		if (!$this->oci->set_charset("utf8")) {
				printf("Utf8 Karakter Seçim Hatası: %s\n", $this->oci->error);
				exit();
		}
  }
	public function affRows() {
		return $this->oci->affected_rows;
	}
	public function closeOCI() {
		$this->oci->close();
	}
	public function closeRS() {
		$this->rs->close();
	}
	public function escape($string) {
		return $this->oci->real_escape_string($string);
	}
	public function killThread($tid) {
		$this->oci->kill($tid);
	}
	public function lastID() {
		return $this->oci->insert_id;
	}
	public function numRows() {
		return $this->rs->num_rows;
	}
	public function query($query) {
		return $this->rs = $this->oci->query($query);
	}
	public function fetch($query) {
		while ( $row = $query->fetch_assoc() ) {
			$results[] = $row;
		}
		return $results;
	}
}
$db = new cSQLi('localhost','root', '', 'php');
$query=$db->query('SELECT * FROM php WHERE id = 1');
print_r($db->fetch($query));
print_r($db->numRows());
print_r($db->affRows());
print_r($db->lastID());
print_r($db->escape("merhaba'lar"));
print_r($db->killThread(1));
print_r($db->closeRS());
print_r($db->oci);
print_r($db->closeOCI());

Çıktısı

Array([0] => Array([id] => 1))
1
1
0
merhaba\'lar
0
0
mysqli Object ( [affected_rows] => 1 [client_info] => mysqlnd 5.0.10 - 20111026 - $Id: xxxxx $ [client_version] => 50010 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.15-log [server_version] => 50615 [stat] => Uptime: 200502 Threads: 1 Questions: 579 Slow queries: 0 Opens: 95 Flush tables: 1 Open tables: 64 Queries per second avg: 0.002 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 95 [warning_count] => 0 )
mysqli_result Object ( [current_field] => 0 [field_count] => 6 [lengths] => [num_rows] => 1 [type] => 0 )

Sql Katman Mysqli Veritabanı Sınıfı

<?php
class SQLKatman {
	var $sunucu;
	var $kullaniciadi;
	var $sifre;
	var $veritabaniadi;
	var $link;
	function SQLKatman ($sunucu = "", $kullaniciadi = "", $pass = "", $veritabaniadi = "") {
		$this->sunucu = $sunucu;
		$this->kullaniciadi = $kullaniciadi;
		$this->sifre = $pass;
		$this->veritabaniadi = $veritabaniadi;
		$this->baglan();
	}
	function baglan(){
		$this->link=mysqli_connect($this->sunucu,$this->kullaniciadi,$this->sifre,$this->veritabaniadi);
		mysqli_set_charset($this->link, "utf8");
	}
	function close(){
		return mysqli_close($this->link);	
	}
	function sorgu($sorgu){
		$this->rs=mysqli_query($this->link,$sorgu);
		if (!$this->rs){
			echo("Hata: Hatalı sorgu \"".$sorgu."\"...<br>");
			echo("Hata: ".mysqli_errno($this->link)."<br>");
			die("Hata: ".mysqli_error($this->link)."<br>");
		}else{
			return $this->rs;
		}
	}
	function veri_ara ($row = 0) {
		return mysqli_data_seek($this->rs,$row);
	}
	function dizi_getir($rs=""){
		if($rs==""){$rs=$this->rs;}
		$this->row=mysqli_fetch_array($rs);
    return $this->row;
	}
	function birles_getir($rs=""){
		if($rs==""){$rs=$this->rs;}
		$this->row=mysqli_fetch_assoc($rs);
    return $this->row;
	}
	function nesne_getir($rs=""){
		if($rs==""){$rs=$this->rs;}
		$this->row=mysqli_fetch_object($rs);
    return $this->row;
	}
	function satir_say($rs=""){
		if($rs == ""){$rs=$this->rs;}
		$rs = $this->rs;
    return mysqli_num_rows($rs);
	}
	function etkilenen_satir() {
		return mysqli_affected_rows($this->link);
	}
	function serbest_sonuc($rs = "") {
		if($rs == ""){$rs=$this->rs;}
		$rs = $this->rs;
    return mysqli_free_result($rs);
	}
}
$s=new SQLKatman("localhost","root","","php");
$s->sorgu("select * from php where id=1");
print_r($s->dizi_getir());echo "<br>";
$s->sorgu("select * from php where id=1");
print_r($s->birles_getir());echo "<br>";
$s->sorgu("select * from php where id=1");
print_r($s->nesne_getir());echo "<br>";
$s->sorgu("select * from php where id=1");
print_r($s->satir_say());echo "<br>";
$s->sorgu("select * from php where id=1");
print_r($s->etkilenen_satir());echo "<br>";
$s->sorgu("select * from php where id=1");
print_r($s->serbest_sonuc());
$s->sorgu("select * from php where id=1");
var_dump($s->veri_ara(2));

?>

Çıktısı

Array ( [0] => 1 [id] => 1 ) 
Array ( [id] => 1 ) 
stdClass Object ( [id] => 1 ) 
1
1
bool(false)