This is a problem that has been haunting me for some time. I have a PHP Web application built on Zend. For a particular feature, I have around 10-20 MySQL queries that get executed within a transaction. Out of these queries, some queries are used to delete values and others to insert values. 99% of the time everything works perfectly. But every now and again, some values are not inserted into the tables. I tried the following to debug this but to no avail:
Logged all MySQL queries by setting log = /var/log/mysql/mysql.log in my.cnf file. The necessary queries are being logged and when I execute them manually, the insert takes place correctly.
Checked the return value after the insert and it returns the primary key of the table.
Checked using newrelic if there was any unusual traffic on the server at these instances, but found out that it was ok
It does not seem to be an issue with the code and I am somehow inclined to believe that it has to be some issues with the MySQL DB.
Updated
Another strange thing that I want to mention with regards to this is:
In one table the primary key is auto-incremented. After the insert; the return value gives me the auto incremented value in the log (eg: 32363). But when I check the table, I can find 32362 and 32364, but not 32363.
Related
I'm trying to make some SQLÂ Server code also run on MySQL, and I just hit this land mine. Google says the normal approach is to simply do your insert and then select last_insert_ID() to find out what got written.
This does not strike me as safe in a multi-user environment, though. There's a narrow window there where another user could insert something and cause a bad return value. How do I safely insert and obtain the key of the inserted record?
From LAST_INSERT_ID(), LAST_INSERT_ID(expr)
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
So unless your inserts for multiple users would happen to be made over the same database connection, you have nothing to worry about.
I have a function on my website that creates multiple requests for insertion into the database. I've tested for a while and haven't come across this issue but it may be because of more users on the system now.
I'm getting a duplicate key error when running an insert on the DB. I don't believe it's an issue with the code, as I'm just creating a new entry. From previous investigation I believe, on a PostgreSQL db at least, the ID is not an auto incremented one but rather something that is set from the ORM. Could a race condition cause two identical ID's to be attempted to be inserted into the DB?
I have a few nagging questions about creating tables:
If I use PHP to create a MySQL function to create a table, I know it works the first time (to create a database for usernames and passwords) but what about the following times when the database sees the code to "create table". It seems to ignore it on my virtual server, but I was just wondering if this is wrong. Does it keep trying to create a new table each time? Is it okay to leave that code in?
Another question I have is, let's say I go into PHPMyAdmin and add a column called "role" (to define the user's role). The sign in page will crash since I added a column in PHPMyAdmin, but if add the column using PHP/MySQL it is perfectly fine. Why is that?
CREATE TABLE is executed each time you run the function. It's better to replace the syntax with CREATE TABLE IF NOT EXISTS.
The keywords IF NOT EXISTS prevent an error from occurring if the
table exists.
If you does not add IF NOT EXISTS it will throw the error.
Reference: http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Please post your code in question to help you with second query.
1.) It depends on the purpose of the table.
If you need to create tables dynamically then your code should check each time
if the table exists:
CREATE TABLE IF NOT EXISTS 'yourTable'
However if you create the table only ones, there is no need to check for existence over and over again, so the code to create these table(s) should execute one time only.
2.) You need to update the function that does the insert or read after adding a column via PHPMyAdmin. It's difficult to answer your second question as I don't know what your functions do.
Do not keep your CREATE TABLE ... statements in your PHP code so that they execute every single time on every single page load. It's unnecessary and error prone. The statements are not being ignored, very likely they are run and are producing errors, and you're simply not checking for errors.
Database creation is a deployment step, meaning when you upload your code to your server, that's the one and only time when you create or modify databases. There are entire toolchains available around managing this process; learn something about automated deployment processes and database schema versioning at some point.
No idea without seeing your code and the exact error message.
I have a corrupted DB and i'm not sure what to do with it. What happen is i have Flex app with a PHP backend where in some parts i need to do multiple inserts in a table, everything seems to work fine, no errors. After running my code i make a select on that table and there's nothing showing up in the table.
I checked everything, i mean everything:
I checked login info.
I trace the code up to the end, the query even shows up in workbench.
I tried inserting with a direct string i made up myself and tested before in workbench, nada .
If i insert data directly from workbench it works.
After data has been inserted from workbench, i run it thru php and instead of updating the db it erase what i have previously inserted in workbench.
So i'm thinking something could be wrong with my computer setup ..., so i set up an environment on another machine. No success here either same thing happens.
I then proceed to boot up an AWS machine with a different environment, same thing here too.
I tried to drop the table and recreate it manually, no success either.
Last thing i did was create the table with a different name, bang that works from everywhere.
Does anybody have clue what could happen here and some way to solve this has i don't want to change the table name, and by the way that table is not a new table and it was working fine before.
If you could give the table name that would be great. Perhaps it's a conflicting keyword you used in the table name that's causing your troubles?
Is it possible to queue client requests for accessing database in MySQL. I am trying to do this for concurrency management. MySQL Locks can be used but somehow I am not able to get the desired outcome.
Effectively what I am trying to do is:
INSERT something in a new row
SELECT a column from that row
Store that value in a variable
The issue comes up when two different clients INSERT at the same time, thus variables for both clients store the value of the last INSERT.
I worked the following alternative, but it failed in a few test runs, and the bug is quite evident:
INSERT
LOCK Table
SELECT
Store
UNLOCK
Thanks!
My best guess is that you have an auto-increment column and want to get its value after inserting a row. One option is to use LAST_INSERT_ID() (details here and here).
If this is not applicable, then please post some more details. What exactly are you trying to do and what queries are being fired?