I am currently working on a project which will have so many queries run a same time. and I am sending data to database in one table and want to check if the data is successfully inserted. I read about mysql_affected_rows(); function and that it will return the values but for example I will have approx 100 tables and as by user going to use my application it will sending data to different table at same times. so the question is how do mysql_affected_rows() will work. because at a same time its gonna be multiple queries in different table. as mysql_affected_rows will need only database connection and it will return the last updation in the database. how do I can check the data update on the table which I want to check. example I have a table name user_info. I want to see if data is inserted successfully in the table or not. as mysql_affected_rows() will return the last database updation as it doesn't matter on which table. it will return the database updatation. but in mean while if some else query hit the database and mysql_affected_rows will return the query as succesfull. I hope my question is clear.
Asked
Active
Viewed 113 times
-2
-
2The function is local for a connection, and reports the amount of rows affected by previous statement. IN single connection the queries are executed synchronously, next stmt cannot be executed until the previous execution is finished. So the interference is not possible in concurrent application. – Akina Dec 29 '22 at 11:50
-
2*I want to see if data is inserted successfully in the table or not.* Simply check insertion operation status. If you do not receive an error then the data is stored successfully. No alternatives. – Akina Dec 29 '22 at 11:51
-
yes I understood your answer very well but in mean time I found a solution that I will check the rows before insert and after insert . if rows after insert are greater then it means it inserted succesfully – Samir Paruthi Dec 29 '22 at 11:52
-
That would only work assuming nothing is deleted in between, and nothing is added by anyone else either. And also it's not as efficient as what is proposed above – ADyson Dec 29 '22 at 12:10
-
Thanks Adyson. I also Understood because as I mentioned multiple queries. multiple users . So it will not work the way I want – Samir Paruthi Dec 29 '22 at 12:26
-
But as far as I got my own answer to check the numbers of rows in table before and after by making two functions that would be much more easier clear and reliable according to my need – Samir Paruthi Dec 29 '22 at 12:29
-
1Why isn't error handling enough? No error = successfully inserted. – MatBailie Dec 29 '22 at 12:31
-
I have just explained why checking it afterwards using a SELECT isn't reliable (a row might have been added but you don't know who added it, and you don't know if someone also deleted something. So a simple count of rows does not prove anything). Use affected_rows to check the outcome of updates and deletes. For inserts, if it doesn't crash then you will know it has worked, because either it succeeds or it crashes, there is no other possible outcome – ADyson Dec 29 '22 at 12:33
-
Adyson users not gonna have authorization to delete the rows. basically I am the admin and only I will be able to delete the rows. while users gonna use the application they are not authorized to delete a record – Samir Paruthi Dec 29 '22 at 12:37
-
Ok. These are constraints that you should be making clear in your question at the start! Then everyone understands the scenario fully – ADyson Dec 29 '22 at 14:46
-
[Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?noredirect=1&lq=1) – brombeer Dec 29 '22 at 16:02
2 Answers
1
Its done with the Error handling if its not returning any error yes its added to database

Samir Paruthi
- 121
- 1
- 3
- 12
0
If you are using AUTO_INCREMENT
with your column, then you can use last_insert_id()
method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command. Display all records from the table using select statement.
CREATE TABLE t (
id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
f VARCHAR(1))
ENGINE = InnoDB;
INSERT INTO t(f) VALUES('a');
SELECT LAST_INSERT_ID();

ADyson
- 57,178
- 14
- 51
- 63