Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which mysqli functions do a client server round trip

Tags:

php

mysql

mysqli

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

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

Depending on the type of query, we use other mysqli function after this like

mysqli_fetch_fields, mysqli_affected_rows, mysqli_insert_id, mysqli_fetch_row

etc. then we close the result object.

Now, is all data retrieved and stored in php memory after step (1)? Or mysqli_fetch_fields, mysqli_insert_id etc makes another call to mysql server?

Reason for asking: Trying to understand how mysqli calls work. But can not find such explanation anywhere for beginners like me.

like image 434
user3282542 Avatar asked Aug 23 '14 07:08

user3282542


People also ask

Which of the following functions are used to prepare and execute MySQLi queries?

mysqli_prepare(): The above MySQLi function is used to prepare a MySQL query for execution. It returns a statement object for further operations and returns FALSE if some error occurs.

What are the types MySQLi function available PHP?

Types of MySQL Functions in PHP: Database connections. Managing Database connections. Performing Queries.

Is MySQLi faster than MySQL?

The installation process with MySQLi not only easy, but is automatic when the PHP5 MySQL package is installed in Windows or Linux. MySQLi performs (slightly) faster than its competition. With non-prepared statements, the system performs ~2.5% faster, and ~6.5% faster with prepared ones.


2 Answers

PHP MySQLi API is built on MySQL C API. So it would be better if you have knowlegdes of it.

Basically, SELECT query could generate large ResultSet and this ResultSet is transfered from Server to Client when you call PHP's mysqli_store_result() (In C API, mysql_store_result()).

  • C API mysql_fetch_row() just returns a pointer to MYSQL_RES* (which is already stored in PHP right after mysql_store_result(). But 'mysqli_fetch_row()` would require some memories to make PHP's array.

  • mysqli_insert_id() (which is last_insert_id() of C API) just returns insert id of MYSQL connection data stucture which means there is no extra memory for insert id.

If you want to know how MySQLi works, I would recommand to learn MySQL C API and see PHP source codes.

like image 110
Jason Heo Avatar answered Sep 20 '22 10:09

Jason Heo


mysqli_query runs the query on the server and returns false is the query failed, true is the query was successful but did not return anything (UPDATE query for example) or a mysqli_result otherwise. That mysqli_result is a class that extends Traversable interface, so yes, it's in memory. All other functions mysqli_fetch_fields, mysqli_affected_rows etc. are just methods in that class so those just read what's already in memory.

For more details, read this: php documentation

like image 40
alez007 Avatar answered Sep 21 '22 10:09

alez007