What mysqli functions do client server round-trip

I want to understand how many client request calls are made for a typical mysqli query?

Step(1) $result = mysqli_query($link, $query); 

Depending on the type of request, we use another mysqli function after this, for example

 mysqli_fetch_fields, mysqli_affected_rows, mysqli_insert_id, mysqli_fetch_row 

etc .. then we close the result object.

Now, all the data is received and stored in php memory after step (1)? Or mysqli_fetch_fields, mysqli_insert_id, etc. Makes another mysql server call?

Reason for request: trying to understand how mysqli calls work. But he cannot find such an explanation anywhere for beginners like me.

+6
source share
3 answers

The PHP MySQLi API is built on the MySQL C API. So it would be better if you knew this.

Basically, a SELECT query can generate a large ResultSet, and that ResultSet is transferred from the server to the client when you call PHP mysqli_store_result() (In C API, mysql_store_result() ).

  • With the API, mysql_fetch_row() simply returns a pointer to MYSQL_RES * (which is already stored in PHP immediately after mysql_store_result() . But mysqli_fetch_row () `will take some memories to create an array of PHP.

  • mysqli_insert_id() (which is the last_insert_id() API C) simply returns the insert identifier of MYSQL connection data data, which means there is no extra memory to insert id.

If you want to know how MySQLi works, I would recommend learning the MySQL C API and looking at the PHP source codes.

+2
source

mysqli_query starts a query on the server and returns false - the query was not completed, true - the query was successful, but returned nothing (for example, an UPDATE query) or mysqli_result. That mysqli_result is a class that extends the Traversable interface, so yes, it's in memory. All other functions are mysqli_fetch_fields, mysqli_affected_rows, etc. - these are just methods in this class, so they just read what is already in memory.

Read more here: php documentation

+1
source

The documentation tells you everything you need to know about mysqli .

mysqli_query execute the query and returns a query object , this object has some methods, and among them are:

  • mysqli_fetch_fields :

    Returns an array of objects representing the fields in the result set.

  • mysqli_affected_rows :

    Returns the number of rows affected by the latest INSERT, UPDATE, REPLACE, or DELETE queries.

  • mysqli_insert_id :

    Returns the automatically generated identifier used in the last query

  • mysqli_fetch_row :

    Get the result string as an enumerated array

Being the entire method of an object, it does not execute sql queries, they simply access the values โ€‹โ€‹of the objects and return different results depending on the method.

+1
source

Source: https://habr.com/ru/post/974240/


All Articles