I need to login with an case-insensitive email ID.My mail ID is stored in an encrypted format I am fetching from a database with something like the following query:
$this->db->select('Name');
$this->db->from('users');
$this->db->where('emailId',"AES_ENCRYPT('{$emailId}','/*awshp$*/') ", FALSE);
$query = $this->db->get();
$result = $query->row();
return $result;
I am using binary but no use
it's have simple logic, While you sign up store hash value of email id after convert it to lowercase. and on login also convert it to lowercase.so if user enter email id in any case,encryption string match.
AES encryption is a two-way algorithm, meaning you can recover the original value, so you can also update existing records that don't conform to the format you want to test.
After you run the update in the database, just apply the suggestions made by Tim Biegeleisen and you should be good to go.
Demo for updating existing records
mysql> CREATE TABLE t1 (
-> email VARBINARY(256)
-> );
Query OK, 0 rows affected (0.31 sec)
mysql> INSERT INTO t1 (email) VALUES
-> (AES_ENCRYPT('MiXeDcAsEdEmAiL#gmail.com','salt')),
-> (AES_ENCRYPT('UPPERCASEDEMAIL#gmail.com','salt')),
-> (AES_ENCRYPT('lowercasedemail#gmail.com','salt'));
Query OK, 3 rows affected (0.09 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> UPDATE t1 SET email = AES_ENCRYPT(LOWER(CAST(AES_DECRYPT(email,'salt') AS CHARACTER)),'salt');
Query OK, 2 rows affected (0.11 sec)
Rows matched: 3 Changed: 2 Warnings: 0
mysql> SELECT CAST(aes_decrypt(email,'salt') AS CHARACTER) lower_cased from t1;
+---------------------------+
| lower_cased |
+---------------------------+
| mixedcasedemail#gmail.com |
| uppercasedemail#gmail.com |
| lowercasedemail#gmail.com |
+---------------------------+
3 rows in set (0.00 sec)
NB
Don't forget to change the update statement to match your column name and the value you use as a salt.
Related
I have a mysql table which has a data structure as follows,
create table data(
....
name char(40) NULL,
...
)
But I could insert names which has characters more than 40 in to name field. Can someone explain what is the actual meaning of char(40)?
You cannot insert a string of more than 40 characters in a column defined with the type CHAR(40).
If you run MySQL in strict mode, you will get an error if you try to insert a longer string.
mysql> create table mytable ( c char(40) );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into mytable (c) values ('Now is the time for all good men to come to the aid of their country.');
ERROR 1406 (22001): Data too long for column 'c' at row 1
If you run MySQL in non-strict mode, the insert will succeed, but only the first 40 characters of your string is stored in the column. The characters beyond 40 are lost, and you get no error.
mysql> set sql_mode='';
Query OK, 0 rows affected (0.00 sec)
mysql> insert into mytable (c) values ('Now is the time for all good men to come to the aid of their country.');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'c' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
mysql> select c from mytable;
+------------------------------------------+
| c |
+------------------------------------------+
| Now is the time for all good men to come |
+------------------------------------------+
1 row in set (0.00 sec)
I recommend operating MySQL in strict mode (strict mode is the default since MySQL 5.7). I would prefer to get an error instead of losing data.
I want to select the values from a column name with non-ascii characters but It cant be possible.
The column name is "Descripón" and I want to remove the "ó" and transform it in à ³.
How can I make the select?
You can use special characters if you use delimited identifiers:
mysql> create table mytable ( `Descripón` text );
Query OK, 0 rows affected (0.03 sec)
mysql> insert into mytable (`Descripón`) values ('hello world');
Query OK, 1 row affected (0.01 sec)
mysql> select `Descripón` from mytable;
+--------------+
| Descripón |
+--------------+
| hello world |
+--------------+
If you want to change the column name to only ASCII characters so you can use it subsequently without delimiting it:
mysql> alter table mytable change column `Descripón` Description text;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select Description from mytable;
+-------------+
| Description |
+-------------+
| hello world |
+-------------+
But you cannot use HTML entities like ó in MySQL queries, sorry.
I have a column in the database's table with data format like this "000011" and an SQL query like:
SELECT *
FROM table
WHERE a = 000010 OR a = 000001 or a = 000011
But if the value is 111111. It will have a lot of OR condition in it.
If data format like 001 (3 digits) it's can use wildcard ( _ )to do this, but I'm stuck when trying to use it in case (6 digits).
Please help me to find other ways?
First, you can use in:
SELECT *
FROM table
WHERE a in (000010, 000001, 000011)
But, I suspect your "data" is actually an integer and you want boolean & or |:
WHERE (a & 000011)
If you want to show data that contains a 1 then use LIKE
SELECT *
FROM table
WHERE a LIKE '%1%'
SELECT *
FROM table
WHERE position('1' in a) > 0
From what I understand you want to find all rows where the binary representation is less than your input. If that is the case, you could use the BINARY function to get the result you want:
mysql> create table bintab (a varchar(10));
Query OK, 0 rows affected (0.07 sec)
mysql> insert into bintab values('000001');
Query OK, 1 row affected (0.04 sec)
mysql> insert into bintab values('000010');
Query OK, 1 row affected (0.01 sec)
mysql> insert into bintab values('000011');
Query OK, 1 row affected (0.01 sec)
mysql> insert into bintab values('000100');
Query OK, 1 row affected (0.00 sec)
mysql> insert into bintab values('000101');
Query OK, 1 row affected (0.00 sec)
mysql> insert into bintab values('000110');
Query OK, 1 row affected (0.01 sec)
mysql> insert into bintab values('000111');
Query OK, 1 row affected (0.00 sec)
mysql> select * from bintab where binary(a) < binary('000100');
+--------+
| a |
+--------+
| 000001 |
| 000010 |
| 000011 |
+--------+
3 rows in set (0.00 sec)
I have a mysql table with a decimal(16,2) field. Seems like the addition operation with another decimal(16,2) field string can cause the Data truncated for column x at row 1 issue, which raises exception in my django project.
I'm aware of multiplication or division operation of that field can cause this issue bacause the result is probably not fit in decimal(16,2) definition, but does the addition and subtraction operation the same?
My MySQL server version is 5.5.37-0ubuntu0.14.04.1. You can reproduce this issue from bellow:
mysql> drop database test;
Query OK, 1 row affected (0.10 sec)
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table t(price decimal(16,2));
Query OK, 0 rows affected (0.16 sec)
mysql> insert into t values('2004.74');
Query OK, 1 row affected (0.03 sec)
mysql> select * from t;
+---------+
| price |
+---------+
| 2004.74 |
+---------+
1 row in set (0.00 sec)
mysql> update t set price = price + '0.09';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update t set price = price + '0.09';
Query OK, 1 row affected, 1 warning (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> show warnings;
+-------+------+--------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------+
| Note | 1265 | Data truncated for column 'price' at row 1 |
+-------+------+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from t;
+---------+
| price |
+---------+
| 2004.92 |
+---------+
1 row in set (0.00 sec)
There are two problems:
You are not storing decimal values, you're trying to store string/varchar, which is converted into double value by mysql, for example following code does not give errors update t set price = price + 0.09; (even executed several times)
Anyway this code gives expected warning (note number) update t set price = price + 0.091; you can change it to update t set price = price + cast(0.091 as decimal(16,2)); of course with cast you can use string values too update t set price = price + cast('0.09' as decimal(16,2));
In my case problem occurs when I try to insert a decimal with 3 digits after the the dot like: 0.xxx on a column defined as DECIMAL(10,2)
I changed it to DECIMAL(10,3) OR used php to enter values like 0.xx on DECIMAL(10,2) table
I have a table that contains a list of the starting letters in a post code e.g. LS for Leeds and SO for Southampton.
I want to match these against a user entered full postcode e.g. LS19 1AB.
I've looked into using LIKE and some regexp stuff in my query but I'm struggling to find a way to do this the right way round.
You can turn the where clause around, and do some string manipulation tricks:
create table post_codes(
post_code varchar(32)
);
mysql> insert into post_codes values("AS");
Query OK, 1 row affected (0.00 sec)
mysql> insert into post_codes values("LS");
Query OK, 1 row affected (0.00 sec)
mysql> select post_code from post_codes where 'LS19 1AB' like CONCAT(post_code,'%');
+-----------+
| post_code |
+-----------+
| LS |
+-----------+
1 row in set (0.00 sec)