Last active
August 29, 2015 14:06
-
-
Save sarpdorukaslan/70856adcfd23a17be938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//mysql sunucuya bağlanma ve veritabanı seçme | |
//mysql | |
mysql_connect('localhost','root','parola'); | |
mysql_select_db('deneme'); | |
//pdo | |
$db = new PDO('mysql:host=localhost;dbname=deneme','root',parola); | |
//sorgu | |
$sqlUyeler = 'select * from uyeler where cins="2"'; | |
//mysql | |
$qryUyeler = mysql_query($sqlUyeler); | |
//pdo | |
$qryUyeler = $db->exec($sqlUyeler); | |
//Döngü : | |
//mysql | |
while($rslUyeler = mysql_fetch_assoc($qryUyeler)) | |
{ | |
echo '<pre>'; | |
print_r($rslUyeler); | |
echo '</pre>'; | |
} | |
//pdo | |
while($rslUyeler = $qryUyeler->fetch(PDO:FETCH_ASSOC)) | |
{ | |
echo '<pre>'; | |
print_r($rslUyeler); | |
echo '</pre>'; | |
} | |
//mysql_quey yerine $db->exec kullanabilirsiniz | |
mysql_query('Delete From Uyeler where id=1'); | |
$db->exec('Delete From Uyeler where id=1'); | |
/* | |
PDO avantajı : özellikle sql injection zaafiyetinden korunmak için parametreli sorgu diye | |
özetleyebilecğeimiz bir yapı kullanabilirsiniz. | |
*/ | |
$cins = 1; | |
$il = 34; | |
$sonuc=$db->prepare("SELECT * FROM uyeler WHERE cins=? and il=?"); | |
$sonuc->execute(array($cins,$il)); | |
while($uye = $sonuc->fetch(PDO:FETCH_ASSOC)) | |
{ | |
echo '<pre>'; | |
print_r($uye); | |
echo '</pre>'; | |
} | |
//Sevdiğim Özelliklerden bir tanesi | |
class Kullanici { | |
public function adSoyad(){ | |
return $this->ad . ' ' . $this->soyad; | |
} | |
public function cinsiyet(){ | |
if ( $this->cinsiyet == 1 ) | |
return 'Kadın'; | |
else | |
return 'Erkek'; | |
} | |
} | |
$qryKullancilar = $db->query("SELECT * FROM kullanicilar"); | |
$qryKullancilar->setFetchMode(PDO::FETCH_CLASS, 'Kullanicilar'); | |
foreach ( $qryKullancilar as $kullanici ){ | |
echo $row->kullanici() . ': ' . $kullanici->cinsiyet() . '<hr />'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment