Created
September 3, 2012 21:25
-
-
Save adililhan/3613704 to your computer and use it in GitHub Desktop.
PHP ile Database’in Tamamında Arama Yapmak
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
class DBSearch { | |
function __construct($keyword){ | |
$this->keyword = $keyword; | |
} | |
function DBConnect(){ | |
$this->db = new PDO("mysql:dbname=test;host=localhost", "root", ""); | |
return $this; | |
} | |
// Veritabanındaki tüm tablolar döndürüldü. | |
function getTables(){ | |
$mtch = $this->db->query('SHOW TABLES'); | |
$this->tables = $mtch->fetchAll(); | |
return $this; | |
} | |
// İstenen tablodaki sütunlar (kolonlar) döndürüldü. | |
function getColumns($table){ | |
$mtch = $this->db->query('SHOW COLUMNS FROM ' . $table); | |
return $mtch->fetchAll(); | |
} | |
function matches($searchSQL){ | |
$src = $this->db->query($searchSQL); | |
if($src->rowCount() > 0){ | |
$view = null; | |
$view .= "------------------------------------------\n"; | |
$view .= "Tablo: " . $this->tbls[0] . "\n"; | |
$view .= "Sorgu: " . $searchSQL; | |
$view .= "\n------------------------------------------\n"; | |
echo $view; | |
} | |
} | |
// Tablolarda arama işlemi burada gerçekleşiyor. | |
function initalize(){ | |
foreach($this->tables as $this->tbls){ | |
$columns = $this->getColumns($this->tbls[0]); | |
$searchSQL = sprintf("SELECT * FROM %s WHERE %s LIKE '%%%s%%'", $this->tbls[0], $columns[0][0], $this->keyword); | |
unset($columns[0][0]); | |
foreach($columns as $clmns){ | |
$searchSQL .= sprintf(" OR %s LIKE '%%%s%%'",$clmns['Field'], $this->keyword); | |
} | |
// Sorgu hazırlandıktan sonra işletilmek üzere matches() gönderiliyor. | |
$this->matches($searchSQL); | |
} | |
} | |
} | |
$search = new DBSearch('deneme'); | |
$search->DBConnect() | |
->getTables(); | |
$search->initalize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment