简体   繁体   中英

mysqli_use_result()

I'm updating an old script using php mysql functions to use mysqli and I've already found an interesting question ( mysqli_use_result() and concurrency ) but it doesn't clarify one thing:

Lets say five users connects to a webpage, they connected at same time and user1 was the first to select the data from a huge MyIsam database (15000 records of forum posts with left join to 'users' and 'attachments' table).

while the php scripts retrieves the result, the other users will not be able to get results, is that right?

Also using the same situation above, when user1 fully received it's result, an 'UPDATE set view_count = INC(1)' query is sent and the table is locked I suppose, and this same query will fail for the other users?

About the article you've quoted. It just means, that you should not do this:

mysqli_query($link, $query);
mysqli_use_result($link);

// lots of 'client processing'
// table is blocked for updates during this
sleep(10) 
mysqli_fetch_* .....

In such situtations you are adviced to do so:

mysqli_query($link, $query);
mysqli_store_result($link);

// lots of 'client processing' 
// table is NOT blocked for updates during this
sleep(10) 
mysqli_fetch_* .....

The article further says, that if a second query will be issued - after calling mysql_use_result() and before fetching the results from the query it will fail. This is meant per connection - per script. So other user's queries won't fail during this.


while the php scripts retrieves the result, the other users will not be able to get results, is that right?

No this is not right. MySQL supports as many parallel connections as you have configured in my.ini max_connections . Concurrent reads are handled by the mysql server. Client code has not to worry about that unless the max connection limit is reached and mysqli_connect() would fail. If your application reaches a point where this happens frequently you'll in most cases first try to tweak your mysql config so that mysql allows more parrallel connections. If a threshold is reached you'll use an attempt like replication or mysql cluster.


Also using the same situation above, when user1 fully received it's result, an 'UPDATE set view_count = INC(1)' query is sent and the table is locked I suppose, and this same query will fail for the other users?

When there are concurrent reads and writes this is of course a performance issue. But the MySQL server handle this for you, meaning the client code has not worry about it as long as connecting to mysql works. If you have really high load you'll mostly use master slave Replication or MySQL cluster.

and this same query will fail for the other users?

A database server usually a bit more intelligent than a plain text file.
So, your queries won't fail. They'd wait instead.

Though I wouldn't use mysqli_use_result() at all. Why not just fetch your results already?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM