⏩ OldSchoolKO ✅ | " VALHALLA " 2.000.000 TL ÖDÜL ⭐ 3 YIL ARADAN SONRA ✅ v.1098 MYKO EFSANESİ ⚔ OFFICIAL 17.05.2024 - 21:00 ⏪
Cuceko

PHP | Basit MySQL Sınıfı

  •         

            

            

            

            

  • HyperFilter | DoS Protection | DDoS Protection | DoS Mitigation | DDoS Mitigation | AntiDoS | AntiDDoS | Proxy Shielding
S Çevrimdışı

saintx

Kayıtlı Üye
12 Mart 2012
250
0
18
28
Merhaba arkadaşlar,



Ufak çaplı projelerimde kullandığım MySQL sınıfını sizlerle paylaşmak istedim.Umarım bir çok arkadaşımızın işine yarayacağından eminim velhasıl bir teşekkürü çok görmezseniz sevinirim.İyi kodlamalar :)



class.mysql.php dosyası içeriği ;

PHP:
<?php

	

	class SXYSQL {

		protected $link;

		protected $settings;

		public $last_query;

		protected $charset;

		public function __construct(array $settings) {

			$this->link = null;

			$this->settings = $settings;

			$this->charset = array(

				'charset' => 'utf8',

				'collation' => 'utf8_general_ci'

			);

			$this->connect();

			$this->set_charset($this->charset);

		}

		public function connect() {

			$this->link = mysql_connect($this->settings['server'],$this->settings['username'],$this->settings['password']) or die(mysql_error());

			if($this->link) {

				if(mysql_select_db($this->settings['database_name'], $this->link)) {

					return;

				}

			} else {

				exit(mysql_error());

			}

		}

		public function query($query) {

			if($this->link) {

				$this->last_query = $query;

				$exec_query = mysql_query($this->last_query) or die(mysql_error());

				if($exec_query) {

					return $exec_query;

				}

			} else {

				$this->connect();

				$this->last_query = $query;

				$exec_query = mysql_query($this->last_query) or die(mysql_error());

				if($exec_query) {

					return $exec_query;

				}

			}

		}

		public function fetch($query_resource) {

			if($this->link) {

				while($fetch = mysql_fetch_assoc($query_resource)) {

					$fetched_data[] = $fetch;

				}

				return $fetched_data;

			} else {

				exit;

			}

		}

		/* (04.11.2012) */

		protected function set_charset(array $charset) {

			$this->query('SET NAMES '.$charset['charset'].' COLLATE '.$charset['collation']);

			$this->query('SET character_set_client = '.$charset['charset']);

			$this->query('SET character_set_results = '.$charset['charset']);

			$this->query('SET character_set_connection = '.$charset['charset']);

			$this->query('SET collation_connection = '.$charset['collation']);

		}

		public function __destruct() {

			mysql_close($this->link);

		}

	}

	

?>



sınıfın kullanımı ;

PHP:
<?php

	

	$settings = array(

	

		'server'           => 'localhost', // MySQL Server Address

		'username'         => 'root', // MySQL Database Username

		'password'         => '', // MySQL Database Password

		'database_name'    => 'test' // MySQL Database Name

	

	);

	

	require 'class.mysql.php';

	

	$mysql = new SXYSQL($settings);



	// Basit bir sorgulama yapıyoruz.

	$query_string = "SHOW TABLES"; // Sorgu cümleciği

	$execute_query = $mysql->query($query_string); // Sorgumuzu çalıştırıyoruz

	$fetch_query = $mysql->fetch($execute_query); // Sorgumuzdan dönen ham MySQL çıktısını dizi (array) tipli bir değişkene çeviriyoruz

	

	// Oluşturduğumuz çıktıyı ekrana bastırıyoruz

	print_r($fetch_query);

	

?>