Good all the time of day.

$result = mysql_query("SELECT * FROM table LIMIT 0, 20",$db); $myrow = mysql_fetch_array($result); do { printf ("%s", $myrow["field"]); } while ($myrow = mysql_fetch_array($result)); 

There is a hosting, on the hosting there is a limit on the number of requests. By the method of "spear" learn not hunting. How many queries to the table does this code actually make? 20?

ps through while possible

 $result = mysql_query("SELECT * FROM table LIMIT 0, 20",$db); $myrow = mysql_fetch_array($result); while ($myrow = mysql_fetch_array($result)) { printf ("%s", $myrow["field"]); } 

But, I think, from this the number of requests will not decrease.

  • The request will be 1, the number of scanned lines is -20. usually a restriction on the number of scanned lines. "Select some_fiel, (select some_field_2 from table2 where table1.id = table2.id) from table1" - 2 requests, number of rows scanned = number of rows of table 1 * number of rows of table2 (actually less but roughly speaking ). - Vladimir Klykov

4 answers 4

No matter how silly and snobbish it sounds (I hate being answered that way) ... change hosting. I once came to terms with limiting the number of databases (idiocy is still), but I see no reason to accept the restriction of the number of queries, especially so tough.

Justification: there is only one request (As Daniel Vendolin correctly noted), but the problem is what exactly is considered a request. If the reference to the DB table is yes, one. If the mysql engine - then twenty-one. If the provider is harmful, it can count subqueries too, then in general something incredible will begin.

As a result, you just have to poke, or call technical support, or search in the user agreement.

  • It is unlikely that there are more than two requests to the engine. The mysql_query () function uses [mysql_store_result ()] [1] to store the results, which means that the data will be processed and transferred to the client as a whole. The request to the engine for each fetch is possible only when using [mysql_unbuffered_query ()] [2] [1]: dev.mysql.com/doc/refman/5.0/en/mysql-store-result.html [2]: ru2.php .net / manual / en / function.mysql-unbuffered-query.php - Ilya Pirogov
  • again. =) If there is no connection to mysql (for example, if the connection file is broken or missing), no functions starting with mysql_ work, except for mysql_connect. In this regard, there is a suspicion that the engine twitches anyway EVERY time, but not every time it is a query to the DB - knes
  • Of course, they will not work, because the corresponding request was not executed correctly. - Ilya Pirogov
  • mysql_real_escape_string also requires a request? =) Warning: mysql_real_escape_string (): Access denied for user 'www-data' @ 'localhost' (using password: NO) in /home/me/www/sandbox.dev/mres.php on line 2 Warning: mysql_real_escape_string () : A link to the server couldn’t be established in /home/me/www/sandbox.dev/mres.php on line 2 - knes
  • one
    The fact that many methods need to be connected to the database one way or another does not mean that mysql_fetch_array () makes a request for each call . Not to be unfounded, a piece of the [method mysql_fetch_row ()] [1]. As you can see, all it does is move the pointer to the next line and return the current one. [1]: pastebin.com/NYpmxcsC - Ilya Pirogov

Request is made 1

$ result = mysql_query ("SELECT * FROM table LIMIT 0,20", $ db);

And this is the processing of requests by php

while ($ myrow = mysql_fetch_array ($ result)) {printf ("% s", $ myrow ["field"]); }

  • one
    And why, then, when mysql is not connected, the system swears at the mysql_fetch_array function? Something is unclean. - knes
  • In terms of swears on mysql_fetch_array? it also needs to transfer some data for processing. But the situation honestly baffled me. Not faced with such restrictions on the servers ... - Daniel Wendolin

mysql_fetch_array works through the active connection this time, two times - its task is to process the data already received . After sampling, a resource comes to us, and mysql_fetch_array is created in order to convert it into a software hash (in our case, into an array). Naturally, the mysql request itself happens, but of course there is no selection from the database ...
In general, it works approximately as follows - you make a request, mysql remembers the result, then you call mysql_fetch ... - mysql converts this result and returns it to the script, as I understand it, and I know it works that way.

    As far as I know, there will be one query to the table. mysql_fetch_array () already works with the result.