Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> zwracanie tabel z zapytania mysql, Funkcja, PHP 4 i 5
aleksander
post 28.11.2004, 14:14:30
Post #1





Grupa: Przyjaciele php.pl
Postów: 742
Pomógł: 0
Dołączył: 14.12.2003
Skąd: Gdańsk, Trójmiasto

Ostrzeżenie: (0%)
-----


Ostatnio potrzebowałem funkcji, która z zapytania SELECT, INSERT, UPDATE i DELETE zwróci mi nazwy wszystkich tabel (do cache zapytan mysql). Zrobiłem taką funkcję i pomyślałem, że umieszczę ją tutaj.

Specyfikacja:
- wsparcie dla zapytań SELECT, INSERT, UPDATE i DELETE (cczyli, tych, które są wykonywane przez skrypty)
- wsparcie dla aliasów (tzn wywalanie ich smile.gif )
- wsparcie dla JOINow

Poniżej daję kod. Przetestować funkcję można na http://www.olek.thc.net.pl/get_tables_from_query.php a ten sam kod jest dostępny na http://www.olek.thc.net.pl/show_code.php?p...m_query_0.6-dev

  1. <?php
  2. /**
  3. * Funkcja zwraca z zapytania mysql nazwy tabel użytych w zapytaniu
  4. *
  5. * Metoda pobiera jako parametr zapytanie mysql a zwraca nazwy tabel w postaci ta
  6. licy.
  7. * Przykład użycia
  8. * <code>
  9. * $query = 'SELECT t1.kolumna1, t2.kolumna2 FROM tabela1 t1, tabela2 t2 WHERE t1.kolumna1 = 
  10. 2.kolumna2';
  11. * $result = get_tables_from_query( $query );
  12. * // array(2) {
  13. * // [0]=>
  14. * // string(7) \"tabela1\"
  15. * // [1]=>
  16. * // string(7) \"tabela2\"
  17. * // }
  18. * </code>
  19. *
  20. * @author Aleksander Dutkowski
  21. * @access public
  22. * @version 1.0-rc
  23. * @copyright Metoda może być dowolnie uzywana i znieniana
  24. * @param string $sql Zapytanie sql
  25. * @return array Nazwy tabel
  26. *
  27. */
  28. function get_tables_from_query( $sql )
  29. {
  30. $sql = strToLower( trim( $sql ) );
  31.  
  32. $which = substr( $sql, 0, 6 );
  33. if( $which == 'select' )
  34. {
  35. /*
  36. * Wycinanie SELECT nazwy_tabel
  37. */
  38. $short_str = strstr($sql, ' from '); // zwoci from i wsio za nim
  39. /*
  40. * Wycinanie wszystkiego po nazwach tabel tj. where, group by, having, order
  41. by i limit
  42. */
  43. $posWhere = strpos($short_str , ' where ');
  44. $posGroup = strpos($short_str , ' group ');
  45. $posHaving = strpos($short_str , ' having ');
  46. $posOrder = strpos($short_str , ' order ');
  47. $posLimit = strpos($short_str , ' limit ');
  48. $arr = array();
  49. if( $posLimit != false )
  50. {
  51. array_push($arr, $posLimit );
  52. }
  53. if( $posOrder != false )
  54. {
  55. array_push($arr, $posOrder );
  56. }
  57. if( $posHaving != false )
  58. {
  59. array_push($arr, $posHaving );
  60. }
  61. if( $posGroup != false )
  62. {
  63. array_push($arr, $posGroup );
  64. }
  65. if( $posWhere != false )
  66. {
  67. array_push($arr, $posWhere );
  68. }
  69. if( count( $arr ) == 1 )
  70. {
  71. $min = $arr[0];
  72. } elseif( count( $arr ) > 1 ) {
  73. $min = min( $arr );
  74. }
  75.  
  76. if( isset( $min ) )
  77. {
  78. $short_str = substr( $short_str, 6, $min - 6 );
  79. } else {
  80. $short_str = substr( $short_str, 6 );
  81. }
  82.  
  83. $str = $short_str;
  84. $joiny = array();
  85.  
  86. while( strPos( $str, 'join' ) != false )
  87. {
  88. $str = substr( $str, strPos( $str, 'join' ) + 5 );
  89. $spacja = strPos( $str, ' ' );
  90. $joiny[] = substr( $str, 0, $spacja );
  91. $str = subStr( $str, $spacja );
  92. }
  93. } elseif( $which == 'insert' )
  94. {
  95. $int = strPos( $sql, 'insert into ' );
  96. if( $int === false )
  97. {
  98.  $short_str = subStr( $sql, strPos( $sql, 'insert ' ) + 7 );
  99. } else {
  100. $short_str = subStr( $sql, $int + 12 );
  101. }
  102. $posNawias = strPos( $short_str, '(' );
  103. $posValues = strPos( $short_str, 'values' );
  104. $posSet = strPos( $short_str, 'set' );
  105. $arr = array();
  106. if( $posNawias != false )
  107. {
  108. array_push($arr, $posNawias );
  109. }
  110. if( $posValues != false )
  111. {
  112. array_push($arr, $posValues );
  113. }
  114. if( $posSet != false )
  115. {
  116. array_push($arr, $posSet );
  117. }
  118.  
  119. if( count( $arr ) == 1 )
  120. {
  121. $min = $arr[0];
  122. } elseif( count( $arr ) > 1 ) {
  123. $min = min( $arr );
  124. }
  125.  
  126. if( isset( $min ) )
  127. {
  128. $short_str = substr( $short_str, 0, $min -);
  129. } else {
  130. $short_str = substr( $short_str, 0 );
  131. }
  132. echo $short_str . '<br />';
  133. } elseif( $which == 'update' ) {
  134. $short_str = subStr( $sql, strPos( $sql, 'update ' ) + 7 );
  135. $short_str = subStr( $short_str, 0, strPos( $short_str, 'set' ) -);
  136. } elseif( $which == 'delete' ) {
  137. $short_str = subStr( $sql, strPos( $sql, 'delete from ' ) + 12 );
  138. $space = strPos( $short_str, ' ' );
  139. if( $space !== false )
  140. {
  141. $short_str = subStr( $short_str, 0, $space );
  142. }
  143. }
  144. /*
  145. * Likwidowanie niepotrzebnych spacji
  146. */
  147. $short_str = str_replace( ', ', ',', $short_str );
  148.  
  149. $tables = explode( ',', $short_str );
  150. /*
  151. * Likwidowanie aliasów
  152. */
  153. foreach( $tables as $key => $val )
  154. {
  155. if( strPos( $val, ' ' ) != false )
  156. {
  157. $tables[$key] = subStr( $val, 0, strPos( $val, ' ' ) );
  158. }
  159. }
  160. if( isset( $joiny ) )
  161. {
  162. $return = array_merge( $tables, $joiny );
  163. } else {
  164. $return = &$tables;
  165. }
  166. /*
  167. Zwracanie tablicy z nazwami tabel
  168. */
  169. return $return;
  170. }
  171. ?>
Go to the top of the page
+Quote Post
Bakus
post 28.11.2004, 20:48:57
Post #2


Administrator serwera


Grupa: Przyjaciele php.pl
Postów: 909
Pomógł: 0
Dołączył: 12.08.2003
Skąd: /var/www/wroclaw.php

Ostrzeżenie: (0%)
-----


  1. INSERT INTO `tp_users` ( `pole1` ) VALUES ( 'dane1' ) ;

Kod
array(1) {
  [0]=>
  string(10) "`tp_users`"
}
a powinno być:
Kod
array(1) {
  [0]=>
  string(10) "tp_users"
}


--------------------
Powrót do przeszłości :)
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 05:07