Skip to content

Instantly share code, notes, and snippets.

@adililhan
Created September 3, 2012 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adililhan/3613704 to your computer and use it in GitHub Desktop.
Save adililhan/3613704 to your computer and use it in GitHub Desktop.
PHP ile Database’in Tamamında Arama Yapmak
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