Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: ADODB - jak wyswietlic liczbe zapytan
Forum PHP.pl > Forum > Gotowe rozwiązania > Skrypty obsługi baz danych
stal-sw
Witam.
Wlasnie zaczalem przygode z ADODB.
I mam takie pytanka:
1.Jak wyswietlic liczbe wykonanych zapytac questionmark.gif?

Ponizej kod mojego pliku index.php
  1. <?
  2. error_reporting  (E_ERROR | E_WARNING | E_PARSE);
  3.  
  4. include&#092;"funkcje.php\";
  5. startTimer();
  6.  
  7. include_once('../adodb/adodb.inc.php');
  8. $ADODB_CACHE_DIR = 'xx.';
  9.  
  10. $server = '';
  11. $user = '';
  12. $pwd = '';
  13. $db = '';
  14.  
  15. $DB = NewADOConnection('mysql');
  16. $DB->Connect($server, $user, $pwd, $db);
  17.  
  18.  
  19. function men() {
  20. Global $DB;
  21.  
  22. $rs = $DB->GetAll(&#092;"SELECT * from menu WHERE parent='0'\");
  23. foreach ($rs as $row) 
  24. {
  25.     $return .='<table width=123 cellspacing=0 cellpadding=0 border=1>';
  26.     $return .='<tr><td><b>'.$row['title'].'</b></td></tr>';
  27.  
  28.     $rs2 = $DB->GetAll(&#092;"SELECT * from menu where parent='\".$row['cid'].\"'\");
  29.     foreach ($rs2 as $row2) 
  30.     {
  31.         if($_GET['dzial'] == $row2['cid']) {
  32.             $return .='<tr><RIGHT><td class=tdactive  width=123 HEIGHT=18>'.$row2['title'].'</td></tr>';
  33.         } else {
  34.             $return .='<tr><RIGHT><td class=tdnormal width=123 HEIGHT=18>
  35.                             <a class=menu href=?dzial='.$row2['cid'].'&op='.$row2['op'].'>'.$row2['title'].'</a></td></tr>';
  36.         }
  37.         
  38.         if($_GET['dzial']==$row2['cid']){
  39.         $rs3 = $DB->GetAll(&#092;"SELECT * from menu where parent='\".$row2['cid'].\"'\");
  40.         foreach ($rs3 as $row3) 
  41.         {    
  42.             if($_GET['dzial'] == $row3['cid']) {
  43.             $return .='<tr><RIGHT><td class=tdactive  width=123 HEIGHT=18>'.$row3['title'].'</td></tr>';
  44.         } else {
  45.             $return .='<tr><RIGHT><td class=tdnormal width=123 HEIGHT=18>
  46.                             <a class=menu href=?dzial='.$row3['cid'].'&op='.$row3['op'].'>'.$row3['title'].'</a></td></tr>';
  47.         }
  48.         }
  49.         }
  50.  
  51.     }
  52. }
  53.  
  54. return $return;
  55. }
  56.  
  57. $abc = endTimer();
  58. print'<table><tr><td><center><a href=# >Czas generowania &nbsp;'.$abc.'</a></td></tr></table>';
  59. ?>

2. Jak zoptymalizowac powyzszy kod aby byl jak najmniej zapytan do bazy ?

3.Ponadto chcialbym wiedziec czy ADODB ma jakas klase do porcjowania wynikow bo np. EZSql ma ma bardzo zaawansowana klase EZREsults ?
Jesli ma to czy moglby ktos podac przyklad jej uzycia questionmark.gif

Z gory dzieki za pomoc.
bigZbig
ad 1. Za kazdym razem gdy wykonujesz $rs = $DB->GetAll(); Zwiększaj wartosc licznika $liczba_wykonanych_zapytan++, a na koniec wyswietl wartosc tej zmiennej. lub poczytaj w dokumentacji adodb na temat fnExecute and fnCacheExecute.
Kod
Examples of fnExecute

Here is an example of using fnExecute, to count all cached queries and non-cached queries, you can do this:

# $db is the connection object
function CountExecs($db, $sql, $inputarray)
{
global $EXECS;

if (!is_array(inputarray)) $EXECS++;
# handle 2-dimensional input arrays
else if (is_array(reset($inputarray))) $EXECS += sizeof($inputarray);
else $EXECS++;
}

# $db is the connection object
function CountCachedExecs($db, $secs2cache, $sql, $inputarray)
{
global $CACHED; $CACHED++;
}

$db = NewADOConnection('mysql');
$db->Connect(...);
$db->fnExecute = 'CountExecs';
$db->fnCacheExecute = 'CountCachedExecs';
:
:
# After many sql statements:`
printf("<p>Total queries=%d; total cached=%d</p>",$EXECS+$CACHED, $CACHED);

The fnExecute function is called before the sql is parsed and executed, so you can perform a query rewrite. If you are passing in a prepared statement, then $sql is an array (see Prepare). The fnCacheExecute function is only called if the recordset returned was cached. The function parameters match the Execute and CacheExecute functions respectively, except that $this (the connection object) is passed as the first parameter.

Since ADOdb 3.91, the behaviour of fnExecute varies depending on whether the defined function returns a value. If it does not return a value, then the $sql is executed as before. This is useful for query rewriting or counting sql queries.

On the other hand, you might want to replace the Execute function with one of your own design. If this is the case, then have your function return a value. If a value is returned, that value is returned immediately, without any further processing. This is used internally by ADOdb to implement LogSQL() functionality.


ad 2. Brak czasu

ad 3. Mysle, ze chodzi ci o SelectLimit();
Kod
SelectLimit($sql,$numrows=-1,$offset=-1,$inputarr=false)

Returns a recordset if successful. Returns false otherwise. Performs a select statement, simulating PostgreSQL's SELECT statement, LIMIT $numrows OFFSET $offset clause.

In PostgreSQL, SELECT * FROM TABLE LIMIT 3 will return the first 3 records only. The equivalent is $connection->SelectLimit('SELECT * FROM TABLE',3). This functionality is simulated for databases that do not possess this feature.

And SELECT * FROM TABLE LIMIT 3 OFFSET 2 will return records 3, 4 and 5 (eg. after record 2, return 3 rows). The equivalent in ADOdb is $connection->SelectLimit('SELECT * FROM TABLE',3,2).

Note that this is the opposite of MySQL's LIMIT clause. You can also set $connection->SelectLimit('SELECT * FROM TABLE',-1,10) to get rows 11 to the last row.

The last parameter $inputarr is for databases that support variable binding such as Oracle oci8. This substantially reduces SQL compilation overhead. Below is an Oracle example:

$conn->SelectLimit("SELECT * FROM TABLE WHERE COND=:val", 100,-1,array('val'=> $val));


The oci8po driver (oracle portable driver) uses the more standard bind variable of ?:

$conn->SelectLimit("SELECT * FROM TABLE WHERE COND=?", 100,-1,array('val'=> $val));

Ron Wilson reports that SelectLimit does not work with UNIONs.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.