This question already has answers here:
How to send a SQL query to database in PHP without waiting for result
(3 answers)
Closed 9 years ago.
Firstly I'm using the deprecated mysql functions (instead of mysqli) knowingly, so please do not tell me I should change to mysqli.
My question is: if I want to do an INSERT or UPDATE and continue processing the PHP script immediately, without waiting for MySQL to complete the task, can I use mysql_unbuffered_query (is that what is does?) or if not, how can I achieve that?
Sorry to break this to you :)
If you use mysqli, with the mysqlnd driver, you can pass a MYSQLI_ASYNC option to the query() method. Unbuffered queries do not help here.
Later on you can use the poll() and reap_async_query() to get to the result.
You can use INSERT...DELAYED for asynchronous insertions (1). I do not believe you can do asynchronous UPDATE's without resorting to spawning another process (2).
(1) but is not available to InnoDB tables
(2) if sticking to the old mysql extension is an absolute requirement
Related
This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Closed 3 years ago.
I moved my php code to a new hosting which missing mysql_nd and a have more than 150 functions that use stmt->get_result() and fetch_assoc.
Is there a function that can do the same work of stmt->get_result() and fetch_assoc and give the exact results so I can fix the problem without having to modify all the functions?
You have two choices.
The first is to contact your provider indicating that you need MySql Native Driver.
The second is to review all your code as a PDO or alternatively MySqli (PDO Council).
In terms of time, with the second option we invest a little more, but for sure you will make your application more stable and secure. I would personally use it for the second hypothesis.
This question already has answers here:
Multiple MYSQL queries vs. Multiple php foreach loops
(5 answers)
Closed 6 years ago.
I've always used simple sql update statements inside a loop when updating multiple records. This means the sql is quick but the database is connected to multiple time.
Lately I've been wondering if it would be much better to use a loop to build up a sql statement that does multiple updates in one go.
Does it make much of a difference? I'm using PHP 5.6 & MySql.
Thank you.
The best solution would is to avoid loops altogether.
If you absolutely need to use a loop, you better do it in the database side, as this will eliminate the overhead related to the PHP/database communication.
As documented under Optimizing UPDATE Statements:
Another way to get fast updates is to delay updates and then do many updates in a row later. Performing multiple updates together is much quicker than doing one at a time if you lock the table.
You may also find the page on Optimizing INSERT Statements helpful and informative.
To summarise, in the most general terms: connecting once and sending a single UPDATE statement is better than connecting once and sending multiple UPDATE statements, which in turn is better than connecting multiple times to send a single UPDATE statement on each connection.
Beyond that, there are a host of techniques that can be harnessed to optimise further—but we would need to know the specific configuration and requirements at hand.
This question already has answers here:
MySQL and PDO: Could PDO::lastInsertId theoretically fail?
(2 answers)
Closed 7 years ago.
I am hoping to not have to write another request to mysqli to get the id of an inserted row.
I need to know if PDO::lastInsertId is an asynchronous thread safe method.
Since this is a functionality question, i will provide a schematic instead of a library i wrote that handles PDO; However, I will provide if necessary.
SCHEMATIC
$connect = mysqli->connect
$is_inserted = $connect->insert(...)
if($is_inserted){
$last_insert_id = $connect->lastInsertId();
}
I need to make sure that a another user will not get someone elses insert_id if they are running the script simultaneously.
The docs for LAST_INSERT_ID() say:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
So, you should have no issues using $connect->lastInsertId().
This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 1 year ago.
I want to switch from MySQL to MySQLi, but I have a very large website.
I read that https://wikis.oracle.com/display/mysql/Converting+to+MySQLi could help me and I read How could I change this mysql to mysqli?. It says that I could replace most of the functions with just adding an 'i' to the function, and that I should start bughunting.
But my website is very complex and large, and it would take a very long time to check if everything works. So: what is the best way to switch from MySQL to MySQLi for a very large website?
Thanks!
There is no easy answer to your question as practically every simple way to do this involved doing things differently when the application was written.
If you have direct calls to mysql_* functions throughout your code and no database abstraction layer where you do your queries through a helper class or function then you will need to edit every command.
You cannot just get away with adding an i to commands like mysql_query as procedurally mysqli_query() requires the first parameter to be the link to the db where with mysql_query() if a connection was given at all, it was a second parameter.
Instead of just changing mysql_query(...) to mysqli_query($link,.....) I would recommend that there is no better time to put a db abstraction layer in place. So use functions eg sql_query() that actually process your queries so in future if you need to change DB again you can just update the db specific commands in one abstraction file. That way if you write a function that wraps mysqli_query then you could be able to simply rename your mysql_query() to your helper function and let the helper function worry about putting the link in there.
Whilst that is the simplest way, it will not bind parameters or prepare statements which is a major factor in preventing sql injection vulnerabilities
Once you have changed all these commands you need to test.
If you have no automated tests written then this is probably a good time to start writing them. Even though you will need to check that every change has worked, if you do it by automated test then you can avoid that pain in future.
you are taking right move becouse the mysql_* function are deprecated for latest php version. you should download a converter for this purpose...
see this and download...mysql to mysqli
This question already has answers here:
PHP: multiple SQL queries in one mysql_query statement
(4 answers)
Closed 8 years ago.
These two PHP MySQL queries work.
mysql_query("DELETE FROM videos WHERE id='10';");
mysql_query("DELETE FROM comments WHERE videoId='10';");
This single query fails due to a MySQL syntax error pertinent to the latter DELETE operation.
mysql_query("DELETE FROM videos WHERE id='10';DELETE FROM comments WHERE videoId='10';");
I've stared hard and can't see the syntax error. What is it?
Not supported by mysql_query see How can I put two queries in one mysql_query? use http://docs.php.net/mysqli.multi-query
You cannot execute multiple queries with mysql_query. If you really want to (security risk!), use mysql_multi_query. (And you should use the newer mysqli_* functions). It's a good idea two embed those two calls in a transaction.
But this looks a lot like you really want to define foreign key constraints. I highly recommend them, if you are already using InnoDB.
Multiples queries are not supported in this function.
http://php.net/manual/en/function.mysql-query.php