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

Sql kodlarımız

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');

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>";
}

?>

Onaya Göre İsim Listelemek

Veritabanımızı oluşturalım

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);

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

<?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>

 

Sorgu Sonucunu Excel’e Aktarmak

<?PHP
error_reporting(4);
class ExportToExcel
{
	function exportWithPage($php_page,$excel_file_name)
	{
		$this->setHeader($excel_file_name);
		require_once "$php_page";
	}
	function setHeader($excel_file_name)
	{
		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=$excel_file_name");
		header("Pragma: no-cache");
		header("Expires: 0");
	}
	function exportWithQuery($qry,$excel_file_name,$conn)//to export with query
	{
		$tmprst=mysql_query($qry,$conn);
		$header="<center><table border=1px><th>Tablo Dökümü</th>";
		$num_field=mysql_num_fields($tmprst);
		while($row=mysql_fetch_array($tmprst,MYSQL_BOTH))
		{
			$body.="<tr>";
			for($i=0;$i<$num_field;$i++)
			{
				$body.="<td>".$row[$i]."</td>";
			}
			$body.="</tr>";	
		}
		$this->setHeader($excel_file_name);
		echo $header.$body."</table";
	}
}
$conn=mysql_connect('localhost','root','')or die('bağlanmadı');
mysql_select_db('test');


//ÖRNEK
$exp=new ExportToExcel();

//$exp->exportWithPage("export_file.php","test.xls");

$qry="select * from qa_users";
$exp->exportWithQuery($qry,"test.xls",$conn);

?>

 

Sorgu Sonrası Yönlendirme ile Güvenlik

Özellikle insert yaparken, flood engellemek için bunu kesinlikle uygulamalısınız.

Veritabanı bağlantınızı baglan.php dosyasında hazırlayın ve mysql_query sorgusunu düzenleyin.

<?php
include("baglan.php");
$sql_kontrol =mysql_query("select or update or insert or delete vs...");
mysql_error();

if (!empty($_SERVER['HTTP_REFERER'])){//yönlendirme
  header("Location: ".$_SERVER['HTTP_REFERER']);
}else{
  header("Location: 404.html?err=referer");
}
?>

 

İki Tablo Arasındaki Farkı Bulmak

Mantık gayet basit. Birbiriyle eşleşmeyen varsa dışarıda kalıyor.

Örneğin böyle bir tablomuz olduğunu düşünürsek

CREATE TABLE urun (`id` int);
INSERT INTO urun (`id`) 
VALUES	(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

CREATE TABLE urundetay (`id` int, `uid` int);
INSERT INTO urundetay (`id`,`uid`) 
VALUES	(1, 1),	(2, 2),	(3, 3),	(4, 4),	(5, 5),	(6, 7);

Yöntem 1

SELECT u.id FROM urun as u 
WHERE NOT EXISTS (SELECT * FROM urundetay as d WHERE d.uid = u.id )

Yöntem 2

SELECT u.id FROM urun as u 
LEFT OUTER JOIN urundetay as d ON u.id = d.uid
where d.uid IS NULL

Çıktısı

+-----+
| id  |
+-----+
|  6  |
|  8  |
|  9  |
|  10 |
+-----+

 

Çift Kayıtları Bulmak

Yapmış olduğum bir örneği paylaşmak istiyorum.

Flood engellememiş iseniz, bu şekilde çift kayıtları silme veya düzenleme imkanınız olabilir.

select f.* from firma as f,
	( select link,count(1) as linkSayisi 
		from firma 
		group by link 
		having count(1)>1
	) as f2 
where f.link=f2.link 
ORDER BY `f`.`link` ASC

veya

SELECT dil,link, COUNT(link) AS adet 
FROM firma 
GROUP BY link,dil 
HAVING ( COUNT(link) > 1 ) 
ORDER BY adet desc