Duplicate ID when inserting - php

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?

Related

Create Table Just Once?

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.

SQLite weird duplicate database

I'm working with SQLite in php, and I was just attempting to add a new column to table, and I went into my table.db file to check if it added it, and I noticed that I have 2 Users tables.
This is really weird to me, because I'm not sure how I have 2 tables with the same name? It also appears to save all of the data twice (Once to either table), which I believe is why I have been having some stability issues.
in my db file I have ā€˛CtableUsersUsersCREATE TABLE Users( and ā€˛%tableUsersUsersCREATE TABLE Users(
So my question is: Is this normal behavior? I'm fairly new to SQLite. And if not, how can I safely remove the 2nd table without deleting the 1st table at the same time?
Some old data might still be in unused parts of some pages. This is harmless.
If it bothers you, run VACUUM.

Values not inserting into MySQL Database table

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.

MySQL error 23000 in same time requests generating custom code PHP - MySQL

I need to generate a code for every record in table A. The code is generated based on the number of rows that exist in the table.
So, if table A has 5 records, the code should be something like C006. If there are 6 records the code should be C007 and so on.
I'm generating the code with no problems except that if the request is done by two or more users at the same time then the generated code is the same since at the time the transaction starts, there are say 5 rows so both processes generate C006. I have solved this by creating a unique index. So if that happens, one gets to save the record and the other user will get the error that the code is not unique.
Now, my concern is: What happens when a hundred or more users make the same request? Is a user going to need to keep making the requests until the code is unique and the record is saved? If that happens, how is it possible to avoid that so that for every request a unique sequential code will be generated and saved?
BTW I'm using Yii and apparently table lock is not possible since Yii uses t as default table alias and when I try to lock a table I need to specify a unique alias.

Queuing SQL requests PHP

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?

Categories