error on local mysql but not on RDS mysql - php

I have a mysql insert query which runs on aws RDS(Live env) but throws an error on my local(local env).
on local I'am using mysql V-5.6
$sql = "INSERT INTO `users` (`id`,
`name`,
`email`,
`pass`)
values('','omi','omi#gmail.com','123123')
id is not null and auto_increment.
The error which i get on local is 'Incorrect integer value: '' for column 'id' at row 1'
but when this executed on live env all the data gets inserted into table.
I cant understand what exactly is happening here. please help. thank you.
DDL of users table.
local
CREATE TABLE `users`
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT '',
`email` varchar(256) NOT NULL,
`pass` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25986 DEFAULT CHARSET=utf8;
Live
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(254) DEFAULT '',
`email` varchar(256) NOT NULL,
`pass` varchar(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26046 DEFAULT CHARSET=utf8;

I believe the error is with those quotes (''). When you want to do an insert with an auto_increment field, you have to use null as argument in the auto_increment field position.
See if this works:
$sql = "INSERT INTO `users`
(`id`, `name`, `email`, `pass`)
values(null,'omi','omi#gmail.com','123123');
EDIT 1
Using null doesn't generate any error because internally the DBMS is prepared to receive such an argument. It understands that is its duty to generate the next number of the sequence and if it hasn't any, 0 (of type integer in your case) is inserted first. I know defining "not null" in the DDL of a field and then using "null" in the DML insert statement for that exact field may look confusing, but it's just the right way to use the auto_increment feature.
From the documentation:
If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers.
Also, if using an empty string as argument in an statement doesn't generate any error, it could maybe be because RDS interface has an internal function that converts empty to null. Something like the nullif function in MySQL.

You can't do it like that. Either dont even mention 'id' or give it null value.
$sql = "INSERT INTO `users` (
`name`,
`email`,
`pass`)
values('omi','omi#gmail.com','123123')
OR:
$sql = "INSERT INTO `users` (`id`,
`name`,
`email`,
`pass`)
values('NULL','omi','omi#gmail.com','123123')

Related

Mysql Create Table If Not Exists else.... Information Schema

Quick one hopefully - not sure where I'm going wrong here but this doesn't seem to work full stop.
Running MySQL query through PHP...
Current code
$uu = mysql_query("
IF EXISTS(SELECT table_name FROM information_schema.tables
WHERE table_schema = 'schema_example' AND table_name = 'test_q')
THEN
insert into `test_q` (code, va_desc, price, category)
values ('$code', '$desc', '$price', '$categ')
on duplicate key update va_desc='$desc', price='$price', category='$categ'
ELSE
CREATE TABLE `test_quote` (
`code` varchar(30) NOT NULL,
`va_desc` text NOT NULL,
`price` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`code`),
UNIQUE KEY `id` (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
insert into `test_q` (code, va_desc, price, category)
values ('$code', '$desc', '$price', '$categ')
END IF;
")or die(mysql_error());
Really appreciate some help on what I need to change, at the moment this does absolutely nothing and doesn't return any specific errors. :/
Having said that if I run it in phpMyAdmin it returns the following (although I can't understand why):
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server
version for the right syntax to use near
'ELSE CREATE TABLE `test_quote`(
`code` varchar(30) NOT NULL,
`va_desc` text NO' at line 7
You don't need to query INFORMATION_SCHEMA. You can use the IF NOT EXISTS option to CREATE TABLE.
mysql_query("CREATE TABLE IF NOT EXISTS `test_q` (
`code` varchar(30) NOT NULL,
`va_desc` text NOT NULL,
`price` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`code`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
mysql_query("insert into `test_q` (code, va_desc, price, category)
values ('$code', '$desc', '$price', '$categ')
on duplicate key update va_desc='$desc', price='$price', category='$categ'");
Also, a primary key is a unique key, you don't need to specify them both when you create the table.
Trying using the following query as if, else and then statements are not supported in the sql query, for that you can stored procedures.
mysql_query("CREATE TABLE IF NOT EXISTS `test_q` (`code` varchar(30) NOT NULL,
`va_desc` text NOT NULL,
`price` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`code`),
);
mysql_query("insert into `test_q` (code, va_desc, price, category)
values ('$code', '$desc', '$price', '$categ')
on duplicate key update va_desc='$desc', price='$price', category='$categ'");

php get the row from serialize data where condition is satisfied

In mysql I have the table look like this
CREATE TABLE IF NOT EXISTS `fl_details` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`phone` varchar(15) NOT NULL,
`country` varchar(100) NOT NULL,
`language_pair` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
and the values inside table is like this. I have jut shown two entries. But there are more then 100 row present.
INSERT INTO `fl_details` (`id`, `first_name`, `last_name`, `email`, `phone`, `country`, `language_pair`) VALUES
(1, 'asdf', 'erty', 'testuser#gmail.com', '12345678909', 'Sri Lanka', 'a:2:{s:6:"source";a:2:{i:0;a:1:{i:0;s:6:"Arabic";}i:1;a:1:{i:0;s:10:"Belarusian";}}s:6:"target";a:2:{i:0;a:2:{i:0;s:8:"Assamese";i:1;s:11:"Azerbaijani";}i:1;a:2:{i:0;s:10:"Belarusian";i:1;s:7:"Bengali";}}}'),
(2, 'asth', 'erui', 'testname#gmail.com', '12312356789', 'India', 'a:2:{s:6:"source";a:1:{i:0;a:1:{i:0;s:7:"English";}}s:6:"target";a:1:{i:0;s:5:"English";}}');
Here you can see I have entered the values for language_pair is in php serialize format. So both source and target language is stored in that column.
Now I want to fetch the total row whose target language is Bengali. So can someone kindly tell me how to do this? Any help and suggestions will be really appreacible. Thanks
you can try something like this
SELECT * FROM fl_details WHERE language_pair REGEXP '.*"target";s:[0-9]+:"English".*'

on duplicate key update results in a high index key

so im using ON DUPLICATE KEY UPDATE when logging in - i am using it at every login because im getting the data from an external page and the users can only update their settings there. thats because the lack of an api by the software on that page.
actually im using this query to update their settings. if the account isnt listed in my database, its getting created with their credentials on success.
my problem is, that if the user isnt listed in my database and they are inserted into it, their id (auto increament) is not 1, 2, 3, 4 and so on. its starting at 32, then it goes to 54, after that 185 and so on. the ID raises so fast. is this an issue in my query or is this actually a bug?
http://puu.sh/8iXv7.png
heres my query
mysqli_query($database, " INSERT INTO `benutzer` (`id`, `login`, `vorname`, `nachname`, `gruppen`, `email`, `adresse`, `telefon`, `telefon2`, `telefon3`, `bemerkungen`)
VALUES (NULL, '".$userdata[0]."', '".$userdata[1]."', '".$userdata[2]."', '".implode(";", $gruppen)."', '".$userdata[3]."', '".$userdata[4]."', '".$userdata[5]."', '".$userdata[6]."', '".$userdata[7]."', '".$userdata[8]."')
ON DUPLICATE KEY UPDATE `vorname` = '".$userdata[1]."', `nachname` = '".$userdata[2]."', `gruppen` = '".implode(";", $gruppen)."', `email` = '".$userdata[3]."', `adresse` = '".$userdata[4]."', `telefon` = '".$userdata[5]."', `telefon2` = '".$userdata[6]."', `telefon3` = '".$userdata[7]."', `bemerkungen` = '".$userdata[8]."'") or die(mysql_error());
aand this is the structure of the table
CREATE TABLE IF NOT EXISTS `benutzer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(32) NOT NULL,
`vorname` text NOT NULL,
`nachname` text NOT NULL,
`gruppen` text NOT NULL,
`email` text NOT NULL,
`adresse` text NOT NULL,
`telefon` text NOT NULL,
`telefon2` text NOT NULL,
`telefon3` text NOT NULL,
`bemerkungen` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `login` (`login`),
KEY `login_2` (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
thanks in advance
It's expected and documented behavior.
If you don't like it - then don't use the tool on the wrong purpose.
However, I wouldn't call it a problem at all. Id is an abstract identifier and lasts up to four billion, which ought to be enough for everyone

Unique "Value" Issue with MySQL database

I am having a small and annoying issue with my MySQL database.
I have a field StudentID that is supposed to be unique for each user but is not mandatory.
So when the user registers and doesn't want to add a studentID - he leaves the field blank. However when a 2nd user leaves the field blank - it counts it as a duplicate value.
Is there any way to fix this ?I tried several methods posted below :
Mysql Error : 1062 - Duplicate entry '' for key
'users_studentid_unique'
1st time (works) :
insert into `users` (`username`, `name`, `email`, `studentid`,
`password`, `updated_at`, `created_at`)
values ('aaaaaa263', 'aaaaaa', 'aaaaaa23d#gmail.com','' ,
'yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2',
'2014-03-20 18:16:54', '2014-03-20 18:16:54')
2nd time (Duplicate value error for studentid) :
insert into `users` (`username`, `name`, `email`, `studentid`,
`password`, `updated_at`, `created_at`)
values ('aaaaaa263', 'aaaaaa', 'aaaaaa23d#gmail.com','' ,
'yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2',
'2014-03-20 18:16:54', '2014-03-20 18:16:54')
Tried :
Checking the NULL value on the studentid table in the database.
Tried inserting into the db using these values instead of the blank field :
NULL
null
UNDEFINED
Still having the same issue though, - Does anyone have any idea how to fix this problem?
Use a NULL value, that's not considered to be a "duplicate" in a unique index. But the column has to allow NULL values, that is, it cannot be defined as NOT NULL.
To insert a NULL value, use the keyword NULL keyword. Note that this is different than the character string 'NULL'
INSERT INTO mytab (a, b) VALUES (NULL, 'NULL')
Column a is assigned the special NULL value. Column b is assigned a character string.
Also, omitting the column from the INSERT list will cause the default value to be assigned to the column. The default value for a column is NULL, unless you've set the default to be something else.
This statement is equivalent to the statement above, in that column a will be assigned the default value defined for column a, which is
INSERT INTO mytab (b) VALUES ('NULL')
The other option is to use an expression to generate the NULL value for you, in the place of the empty string.
Given a description of your current logic, if the user_id column allows for NULL values, then you could translate the empty string to NULL
insert into `users` (`username`, `name`, `email`, `studentid`,
`password`, `updated_at`, `created_at`)
values ( ?, ?, ?, NULLIF( ? ,''), ?, ?, ?)
The NULLIF(x,y) function is shorthand for IF x = y THEN NULL ELSE x END.
You can set the student id to a timestamp if it's null, then check if its numeric to see if it's really set or not.
<?php
$student_id = is_null($student_id) ? time() : $student_id;
Your request :
insert into `users` (`username`, `name`, `email`, `studentid`,
`password`, `updated_at`, `created_at`)
values ('aaaaaa263', 'aaaaaa', 'aaaaaa23d#gmail.com','$student_id' ,
'yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2',
'2014-03-20 18:16:54', '2014-03-20 18:16:54')
Then to get the users back :
SELECT *, if(concat('','student_id' * 1) = 'student_id', 0, 1) as is_student_id_valid from ...
Then is_studient_id_valid field will tell you if this id is valid or not.
I have tried to recreate your problem. Alas, it works fine here.
However, i supply all the information so that you can at least check where your setup is different.
Create table statement: Please check all of: the Mysql Version, the table engine, the primary key and the index on the 'studentid' column.
/*
SQLyog Community v10.2
MySQL - 5.5.16 : Database - testmysql
*********************************************************************
*/
/*Table structure for table `users` */
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`username` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`studentid` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
`password` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`username`),
UNIQUE KEY `studentid_uidx` (`studentid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Typical insert with a null studentid...
INSERT INTO `users` (`username`, `name`, `email`, `studentid`,
`password`, `updated_at`, `created_at`)
VALUES ('aaaaaa004', 'aaaaaa', 'aaaaaa23d#gmail.com', NULL,
'yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2',
'2014-03-20 18:16:54', '2014-03-20 18:16:54')
query result
username name email studentid password created_at updated_at
aaaaaa001 aaaaaa aaaaaa23d#gmail.com (NULL) yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2 2014-03-20 18:16:54 2014-03-20 18:16:54
aaaaaa002 aaaaaa aaaaaa23d#gmail.com (NULL) yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2 2014-03-20 18:16:54 2014-03-20 18:16:54
aaaaaa003 aaaaaa aaaaaa23d#gmail.com stid01 yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2 2014-03-20 18:16:54 2014-03-20 18:16:54
aaaaaa004 aaaaaa aaaaaa23d#gmail.com (NULL) yZPeKRV0C0xbr/QCI1w/Y.0Z1qSGnJeivgP71epaHpT139g65hAf2 2014-03-20 18:16:54 2014-03-20 18:16:54

create and insert in a single query

<?php
mysql_query("CREATE TABLE IF NOT EXISTS test.$p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO $p ( `title`, `colum`, `ord`, `tex`, `search`, `count`, `order`) VALUES
('$a', '$b', '$c', '$d', '$f', '$h', '$g'); ");
?>
I am working in a PHP language . $r is my database and $p is my table name
In this I am creating a table , if table is not created and if the table is created then i want to insert the values in the respective column given above but I am not good at mysql_query so I don't know where to add the insert query
I found a solution for my problem but this code is properly working in the phpmyadmin but when i run this code using php , it show me nothing inthe database
You can not execute two queries with a single mysql_query().
Make another call to mysql_query() with the INSERT query as the parameter.
If you absolutely must execute multiple queries in a single function call, change your mysql engne to mysqli, then use mysqli_multi_query() like so:
mysqli_multi_query ($link, 'query1;query2;query3;...');
Please keep in mind that although both approaches issue queries sequentially, their execution is not atomic. If you need atomicity, use a TRANSACTION.
The 13.1.17. CREATE TABLE Syntax can do something like:
CREATE TABLE IF NOT EXISTS `table1` (
`col1` INT (11) DEFAULT NULL,
`col2` INT (11) DEFAULT NULL
)
SELECT 1 `col1`, 2 `col2`;
and should work with mysql_query

Categories