Pdo Veritabanı Sınıfı

PHP
313 lines
<?php
// bağlantı ayarları///////////////////////////////////////////////////////////////////////////////
Class dbConfig extends PDO {
protected $dbConfig = array();
protected function createConfig() {
$this->dbConfig['host'] = 'localhost';
$this->dbConfig['username'] = 'root';
$this->dbConfig['password'] = '';
$this->dbConfig['dbname'] = 'test';
}
}
// veritabanı sınıfı //////////////////////////////////////////////////////////////////////////////
Class Database extends dbConfig {
protected $connection;
function __construct() {
parent::createConfig();
try{
$dsn = 'mysql:host=' . $this->dbConfig['host'] . ';dbname=' . $this->dbConfig['dbname'];
$this->connection = new PDO($dsn, $this->dbConfig['username'], $this->dbConfig['password']);
$this->connection->query("SET NAMES utf8");
$this->connection->query("SET CHARACTER SET utf8");
$this->connection->query("SET COLLATION_CONNECTION = 'utf8_general_ci");
return true;
}catch(PDOException $error){
$errorMesage = 'Hata : Veritabanı bağlantısı kurulamadı !<br>Hata Mesajı =>'.$error->getMessage();
return $errorMesage;
}
}
public function selectOr($table, $array = null) {
if($array == null){
$sql = "SELECT * FROM ".$table;
}else{
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."' or ";
}
}
$sql = "SELECT * FROM ".$table." WHERE ".$sqlString;
}
$select = $this->connection->query($sql);
if ($select) {
$row = $select->fetchAll();
return $row;
} else {
return false;
}
}
public function selectAnd($table, $array = null) {
if($array == null){
$sql = "SELECT * FROM ".$table;
}else{
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."' and ";
}
}
$sql = "SELECT * FROM ".$table." WHERE ".$sqlString;
}
$select = $this->connection->query($sql);
if ($select) {
$row = $select->fetchAll();
return $row;
} else {
return false;
}
}
public function selectOrLimit($table, $array = null, $limit, $start = null) {
if($start == null){
$limitStr = "LIMIT ".$limit;
}else{
$limitStr = "LIMIT ".$start.", ".$limit;
}
if($array == null){
$sql = "SELECT * FROM ".$table." ".$limitStr;
}else{
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."' or ";
}
}
$sql = "SELECT * FROM ".$table." WHERE ".$sqlString." ".$limitStr;
}
$select = $this->connection->query($sql);
if ($select) {
$row = $select->fetchAll();
return $row;
} else {
return false;
}
}
public function selectAndLimit($table, $array = null, $limit, $start = null) {
if($start == null){
$limitStr = "LIMIT ".$limit;
}else{
$limitStr = "LIMIT ".$start.", ".$limit;
}
if($array == null){
$sql = "SELECT * FROM ".$table." ".$limitStr;
}else{
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."' and ";
}
}
$sql = "SELECT * FROM ".$table." WHERE ".$sqlString." ".$limitStr;
}
$select = $this->connection->query($sql);
if ($select) {
$row = $select->fetchAll();
return $row;
} else {
return false;
}
}
public function insert($table, $array) {
$columns = implode(", ", array_keys($array));
$values = implode("',' ", array_values($array));
$sql = "INSERT INTO ".$table."(".$columns.") VALUES ('".$values."')";
$insert = $this->connection->query($sql);
if ($insert) {
return $this->connection->lastInsertId($table);
} else {
return false;
}
}
public function update($table, $id, $array) {
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."', ";
}
}
$sql = "UPDATE ".$table." SET ".$sqlString." WHERE id=" . $id;
$update = $this->connection->query($sql);
if ($update) {
return true;
} else {
return false;
}
}
public function delete($table, $id) {
$sql = 'DELETE FROM ' . $table . ' WHERE id=' . $id;
$delete = $this->connection->exec($sql);
if ($delete) {
return true;
} else {
return false;
}
}
public function query($sql) {
$query = $this->connection->query($sql);
if ($query) {
return $query;
} else {
return false;
}
}
public function count($table, $array = null) {
if($array == null){
$sql = "SELECT count(*) from " . $table;
}else{
$columns = array_keys($array);
$values = array_values($array);
$sqlString = "";
for($i=0;$i<count($columns);$i++){
if($i==count($columns)-1){
$sqlString .= $columns[$i]." = '".$values[$i]."' ";
}else{
$sqlString .= $columns[$i]." = '".$values[$i]."' and ";
}
}
$sql = "SELECT count(*) from " . $table. " WHERE ". $sqlString;
}
$count = $this->connection->prepare($sql);
$count->execute();
return $count->fetchColumn();
}
function __destruct() {
$this->connection = null;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
$db = new Database;
//Insert Sorgusu
$userArray = array(
'username' => 'deneme',
'password' => md5('123456'),
'full_name' => 'Deneme DENEME',
'email' => 'deneme@deneme.com',
'auth' => 'user',
'lang' => 'tr'
);
$table = "users";
$db->insert($table, $userArray);
//Update Sorgusu
$userArray = array(
'username' => 'deneme1',
'password' => md5('1234'),
'lang' => 'en'
);
$id=3;
$table = "users";
$db->update($table,$id, $userArray);
//Delete Sorgusu
$id=3;
$table = "users";
$db->delete($table,$id);
//Count Sorgusu Tüm Tablo
$table = "users";
$db->count($table);
//Count Sorgusu Sütun
$sayılacak = array(
'lang' => 'tr',
'auth' => 'admin'
);
$table = "users";
$db->count($table,$sayılacak);
//Select and Sorgusu
$login = array(
'username' => 'username',
'password' => md5('password');
$user = $db->selectAnd("users", $login);
print_r($user);
//Select or Sorgusu
$yetkili = array(
'username' => 'username',
'auth' => 'admin');
$yetkili = $db->selectOr("users", $yetkili);
//eğer kayıt varsa herzaman iki boyutlu bir dizi döner yoksa boş döner
print_r($yetkili);
//Select or Limit Sorgusu
$yetkili = array(
'username' => 'username',
'auth' => 'admin');
$yetkili = $db->selectOrLimit("users", $yetkili,5);
//eğer kayıt varsa herzaman iki boyutlu bir dizi döner yoksa boş döner ve limit kadar veri getirir
print_r($yetkili);
//Limitte başlangıç değeri
$yetkili = array(
'username' => 'username',
'auth' => 'admin');
$başlangıc= 4;
$limit=7;
$yetkili = $db->selectOrLimit("users", $yetkili, $limit, $başlangıc);
//eğer kayıt varsa herzaman iki boyutlu bir dizi döner yoksa boş döner ve limit kadar veri getirir
print_r($yetkili);
/*Eğer paremetre göndermek istemiyorsak şu şekilde işlem ysparız selectOrLimit("users", null, $limit, $başlangıc)*/
//Select and Limit Sorgusu
$user= array(
'username' => 'username',
'password' => md5('password'));
$user = $db->selectAndLimit("users", $user,1);
print_r($yetkili);
//Manuel Sorgu
$sql = "SELECT (ad,soyad) FROM user ad='fatih' and soyad='göl'";
$gelen = $db->query($sql);
$gelen = $gelen->fetchAll();
print_r($gelen);
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

PDO ile Veritabanı Bağlantısı ve Listeleme

Sql kodlarımız

MySQL
10 lines
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
PRIMARY KEY (`uye_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Tablo döküm verisi `users`
--
INSERT INTO `users` (`user_id`, `username`) VALUES (1, 'admin'),(2, 'test'),(3, 'user');
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Php kodarımız

<?php 
///////////////////////BAGLANTI FONKSIYONUMUZ//////////////////////////
function pdo_mysql_baglan($HOST,$NAME,$USER,$PASS,$CHAR){
return $GLOBALS["baglan"]=array(0=>"mysql:host=$HOST;dbname=$NAME;charset=$CHAR",1=>"$USER",2=>"$PASS");
}
////////////////////////////////////////////////////////////////////////////////////

pdo_mysql_baglan("localhost","wordpress","root","123456","utf8");
$pdo = new pdo($baglan[0],$baglan[1],$baglan[2]);

$query = $pdo->query("SELECT * FROM users");
foreach( $query->fetchAll(PDO::FETCH_ASSOC) as $data){
	echo $data["username"];
	echo "<br>";
}

?>

Şifre Zorluğu Gösterimi

HTML
161 lines
<html>
<head>
<style>
body
{
/*ie needs this*/
margin:0px;
padding:0px;
/*set global font settings*/
font-size:10px;
font-family:Tahoma,Verdana,Arial;
}
a:hover
{
color:#fff;
}
#user_registration
{
border:1px solid #cccccc;
margin:auto auto;
margin-top:100px;
width:400px;
}
#user_registration label
{
display: block; /* block float the labels to left column, set a width */
float: left;
width: 70px;
margin: 0px 10px 0px 5px;
text-align: right;
line-height:1em;
font-weight:bold;
}
#user_registration input
{
width:250px;
}
#user_registration p
{
clear:both;
}
#submit
{
border:1px solid #cccccc;
width:100px !important;
margin:10px;
}
h1
{
text-align:center;
}
#passwordStrength
{
height:10px;
display:block;
float:left;
}
.strength0
{
width:250px;
background:#cccccc;
}
.strength1
{
width:50px;
background:#ff0000;
}
.strength2
{
width:100px;
background:#ff5f5f;
}
.strength3
{
width:150px;
background:#56e500;
}
.strength4
{
background:#4dcd00;
width:200px;
}
.strength5
{
background:#399800;
width:250px;
}
</style>
</style>
<script>
function passwordStrength(password)
{
var desc = new Array();
desc[0] = "Çok Zayıf";
desc[1] = "Zayıf";
desc[2] = "İdare Eder";
desc[3] = "Orta";
desc[4] = "Güçlü";
desc[5] = "Çok Güçlü";
var score = 0;
//if password bigger than 6 give 1 point
if (password.length > 6) score++;
//if password has both lower and uppercase characters give 1 point
if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
//if password has at least one number give 1 point
if (password.match(/d+/)) score++;
//if password has at least one special caracther give 1 point
if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
//if password bigger than 12 give another 1 point
if (password.length > 12) score++;
document.getElementById("passwordDescription").innerHTML = desc[score];
document.getElementById("passwordStrength").className = "strength" + score;
}
</script>
</head>
<body>
<form method="post" action="" id="user_registration" name="user_registration">
<p><h1>Kayıt Formu</h1></p>
<p>
<label for="user">Kullanıcı Adı</label><input type="text" name="user" id="user"/>
</p>
<p>
<label for="pass">Şifre</label><input type="password" name="pass" id="pass" onkeyup="passwordStrength(this.value)"/>
</p>
<p>
<label for="passwordStrength">Şifre Zorluğu</label>
<div id="passwordDescription">Şifre Girilmedi</div>
<div id="passwordStrength" class="strength0"></div>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Kayıt Ol">
</p>
</form>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

E-Mail Adresi Doğruluğunu Kontrol Etmek

PHP
17 lines
<?php
function mailKontrol($mail) {
if ( filter_var($mail, FILTER_VALIDATE_EMAIL) ) {
echo "Doğru email adresi";
} else {
echo "Hatalı email adresi";
}
}
if($_POST){
mailKontrol($_POST["email"]);
}
?>
<form method="post" action="">
<input type="email" name="email"/>
<button type="submit">Kontrol Et</button>
</form>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

Php Dosyalarını Düzenlemek

Boş bir dosya.php oluşturun. index.php içerisine aşağıdaki kodları ekleyerek dosya.php içeriğini düzenleyebilirsiniz.

PHP
63 lines
<?php session_start();
if(!isset($_SESSION["login"])){
echo "Bu sayfayı görüntüleme yetkiniz yoktur.";
}else{
$dizin = "";
$uzanti = "php"; //hangi uzantı?
//Uzantı alma fonksiyonu
function ext($text) {
$text = strtolower(pathinfo($text, PATHINFO_EXTENSION));
return $text; }
echo 'Lütfen Bir Sayfa Seçin:';
echo '<hr>';
if ($handle = opendir("$dizin") or die ("Dizin acilamadi!")) {
while (false !== ($file = readdir($handle))) {
$filetype = ext($file);
if(is_file($dizin."/".$file) && $filetype == "$uzanti") { //eger bir dosya ise ve bizim belirlediğimiz uzantıya sahipse
$class = ($css % 2) ? "satir1" : "satir2"; // her satira farklı class
echo '
<div class="'.$class.'"><a href="edit.php,$sayfa='.$file.'">'.$file.'</a> </div>';
$css++;
}
} //while end
closedir($handle);
}
}
////////////////////////////////////////
$sayfa="dosya.php";
if(!empty($_POST['icerik']))
{
$yazdir = file_put_contents($sayfa, $_POST['icerik']);
if(!$yazdir)
echo 'yazdırılamadı';
else
echo 'başarıyla yazdırıldı';
exit;
}
$icerik = file_get_contents($sayfa);
echo '
<form action="" method="post">
<textarea style="width: 800px; height: 600px;" name="icerik">', $icerik, '</textarea>
<br />
<input type="submit" value="Yayınla" />
<a href="index.php">İptal</a>
</form>';
?>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

Seçenek Seçerek Otomatik Yönlendirme

HTML
6 lines
<SELECT onchange='window.location.href="?sehir="+this.options[this.selectedIndex].value'>
<OPTION selected VALUE="0">Bir şehir seçmelisiniz</OPTION>
<OPTION VALUE="3">Konya</OPTION>
<OPTION VALUE="2">Ankara</OPTION>
<OPTION VALUE="1">Eskişehir</OPTION>
</SELECT>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

Onaya Göre İsim Listelemek

Veritabanımızı oluşturalım

MySQL
14 lines
CREATE TABLE uretim
(
id int auto_increment primary key,
musteri varchar(255),
adet int(11),
onay int(1)
);
INSERT INTO uretim (id,musteri,adet,onay)
VALUES
(1, '1.Müşteri',5,0),
(2, '2.Müşteri',8,1),
(3, '3.Müşteri',2,2),
(4, '4.Müşteri',7,3);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

baglan.php adlı dosyamızdan veritabanı bağlantımızı oluşturmuş varsayıp, kodlarımızı ekliyoruz.

PHP
82 lines
<?php include "baglan.php";
?>
<table border="1" bordercolor="#111111" width="100%">
<tr>
<td width="25%" >
<table border="1" bordercolor="#111111" width="100%">
<tbody>
<th>Bekleyen</th>
</tbody>
<thead>
<tr>
<td>Müşteri Adı</td><td>Adet</td>
</tr>
<?php
$veri=mysql_query("select * table uretim Where onay = 1");
while($v=mysql_fetch_array($veri)){
echo '<tr><td>'.$v["musteri"].'</td><td>'.$v["adet"].'</td>';
}
?>
</tr>
</thead>
</table>
</td>
<td width="25%" >
<table border="1" bordercolor="#111111" width="100%">
<tbody>
<th>Kaplama</th>
</tbody>
<thead>
<tr>
<td>Müşteri Adı</td><td>Adet</td>
</tr>
<?php
$veri=mysql_query("select * table uretim Where onay = 2");
while($v=mysql_fetch_array($veri)){
echo '<tr><td>'.$v["musteri"].'</td><td>'.$v["adet"].'</td>';
}
?>
</tr>
</thead>
</table>
</td>
<td width="25%" >
<table border="1" bordercolor="#111111" width="100%">
<tbody>
<th>Taşlama</th>
</tbody>
<thead>
<tr>
<td>Müşteri Adı</td><td>Adet</td>
</tr>
<?php
$veri=mysql_query("select * table uretim Where onay = 3");
while($v=mysql_fetch_array($veri)){
echo '<tr><td>'.$v["musteri"].'</td><td>'.$v["adet"].'</td>';
}
?>
</tr>
</thead>
</table>
</td>
<td width="25%" >
<table border="1" bordercolor="#111111" width="100%">
<tbody>
<th>Sevkiyat</th>
</tbody>
<thead>
<tr>
<td>Müşteri Adı</td><td>Adet</td>
</tr>
<?php
$veri=mysql_query("select * table uretim Where onay = 1");
while($v=mysql_fetch_array($veri)){
echo '<tr><td>'.$v["musteri"].'</td><td>'.$v["adet"].'</td>';
}
?>
</tr>
</thead>
</table>
</td>
</tr>
</table>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

 

Excel Dosyası Oluşturmak

PHP
23 lines
<?php
header("Content-type:application/vnd.ms-excel; charset=utf-8");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: inline; filename="dosya.xls"');
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sayfa</title></head>
<body>
<table>
<tr>
<td align="left">BAŞLIK 1</td>
<td align="left">BAŞKIK 2</td>
<td align="left">BAŞLIK 3</td>
</tr>
<tr>
<td align="left">içerik</td>
<td align="left">içerik</td>
<td align="left">içerik</td>
</tr>
</table>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX