Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Sterownik]Sterownik do Mysql
marcio
post
Post #1





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


WItam pisze sobie sterownik do bazy Mysql dla zapotrzebowan mojego cms'a i mysle tak sobie czy takie rozwiazane jest godne uzycia np:
  1. <?php
  2. function DbSqlSelect($columns = array('*'), $table = '', $wh = array(''), $other = '') {
  3.  
  4. if(count($columns) == 1) $column = $columns[0];
  5. else if(count($columns) > 1) $column = implode(',', $columns);
  6. else $column = '*';
  7.  
  8. if(count($wh) == 1) {
  9.  
  10. $key_tab = array_keys($wh);
  11. $where = $key_tab[0];
  12.  
  13.  foreach($wh as $argv => $args)
  14.  
  15.  $countArgs = count($args);
  16.  
  17.   for($i = 0; $i < $countArgs; $i++) {
  18.  
  19.    if(is_int($args[$i])) $where .= "(int)$args[$i]";
  20.    else if(is_string($args[$i-1])) $where .= "mysql_real_escape_string('$args[$i]')";
  21.    else $where .= $args[$i];
  22.  
  23.   }
  24. }
  25.  
  26. else if(count($wh) > 1) {
  27.  
  28. $where = '';
  29.  
  30.  foreach($wh as $key => $value) {
  31.  
  32.  $where .= $key;
  33.  $countValue = count($value);
  34.  
  35.   for($i = 0; $i < $countValue; $i++) {
  36.  
  37.   if(is_int($value[$i])) $where .= "(int)$value[$i]";
  38.   else if(is_string($value[$i-1])) $where .= "mysql_real_escape_string('$value[$i]')";
  39.   else $where .= $value[$i];
  40.  
  41.     }
  42.   }
  43. }
  44. else $where = '';
  45.  
  46. if(count($wh) != 0 && !empty($other)) $SqlSelect = 'select '.$column.' from '.$table.' where '.$where.' '.$other.'';
  47.  
  48.  else if(count($wh) != 0 && empty($other)) $SqlSelect = 'select '.$column.' from '.$table.' where '.$where;
  49.  
  50.   else if(count($wh) == 0 && !empty($other)) $SqlSelect = 'select '.$column.' from '.$table.' '.$other;
  51.  
  52.    else $SqlSelect = 'select '.$column.' from '.$table;
  53.  
  54. return $sql = mysql_query($SqlSelect);
  55.  
  56. }
  57. ?>

A tak np wywoluje zapytania:
  1. <?php
  2. /*Wiecej warunkow where*/
  3.  
  4. //Wynik: select * from m_users where id=(int)1 and login=mysql_real_escape_string('marcio')
  5. $zawartosc .= DbSqlSelect(array('*'), 'm_users', array('id' => array('=', 1, ' and '), 'login' => array('=','marcio')));
  6.  
  7. /*Dla jednego warunku where*/
  8.  
  9. //Wynik: select * from m_users where login=mysql_real_escape_string('marcio')
  10. $zawartosc .= DbSqlSelect(array('*'), 'm_users', array('login' => array('=', 'marcio')));
  11.  
  12. /*Przyklad dla Sys. news'ow*/
  13.  
  14. $ris = DbSqlSelect(array('*'), 'news', array(), "order by id desc limit ".$strona*$liczba.",".$liczba."");
  15. $dane = DbSqlRecordsAssoc($ris);
  16. $count_dane = count($dane);
  17. for($i = 0; $i < $count_dane; $i++) {
  18.  
  19.  $wynik2 = DbSqlSelect(array('id'), 'komentarze', array('id_news' => array('=', $dane[$i]['id']))); //SPosob z uzyciem funk. DbSqlSelect
  20.  $wynik2 = DbSqlQuery('select * from komentarze where id_news = '.(int)$dane[$i]['id'].''); //Zwykly select
  21.  $ile_comm = DbSqlNumRows($wynik2);
  22.  $ile_comm = DbSqlCountRows('select count(id) from komentarze where id_news = '.$dane[$i]['id'].'');
  23. }
  24. ?>

Ilosc komentarzy mozna utrzymac w 2 sposoby albo za pomoca DbSqlQuery() ktory pobiera resource zapytania lub za pomoca DbSqlCountRows() ktore liczy normalnie.

Nie musze filtrowac zmiennych ani nic wydaje mi sie tez to bardziej czytelne i w ogole.

Na podstawie funckji DbSqlSelect() chcialbym zrobic tez wlasna skladnie dla zapytan Update,Insert i Delete, oczywiscie DbSqlSelect() nie oferuje wszyskich przypadkow zapytan ale dla podstawowych wystarcza.

P.S pojawil sie problem mam funkcje:
  1. <?php
  2. function DbSqlNumRows($Sql) {
  3.  
  4. return $sql = mysql_num_rows($Sql);
  5. }
  6. ?>

Gdy jej wynik przypisze jakiejs zmiennej to Mysql zwraca mi blad ze jest zly identyfikator polaczenia a jesli dam odrazu do echo DbSqlNumRows($id_zapytania) to jest good dlaczego tak sie dzieje, po modyfikacji funkcji DbSqlSelect() nastapil taki problem a przeciez jest return w funkcji (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) moze juz jest pozno i nie kontaktuje.

P.S2 oczywiscie nie chce oceny co do kodu bo wiem ze ameryki nie odkrylem jednak nie bylem pewny gdzie umiescic temat jako ze jest troche dwuznaczny.

Ten post edytował marcio 6.03.2009, 00:07:25
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.09.2025 - 23:40