Yii2 encrypt and Mysql - mysql

I hope someone can help me with this.
I am saving a personal data into MYSQL by encrypting using Yii2 encrypt/decrypt function, testing without saving the content into the DB works.
However saving the content into the DB and retrieving it back, seems something goes wrong because I am not able to decrypt it.
My MYSQL scheme is this
CREATE TABLE IF NOT EXISTS `customer` (
`customer_id` int(11) NOT NULL,
`customerHash` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`customerDate` date DEFAULT NULL,
`customerTime` time DEFAULT NULL,
`customerUpdated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customerEmail` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPassword` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`customerToken` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerAddress` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPhone` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPostcode` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerName` blob,
`customerSurname` varchar(255) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Yii code
Encrypt
$request=Yii::$app->request;
$hash=Yii::$app->getSecurity()->generateRandomString();
$model->customerHash=$hash;
$model->customerName=Yii::$app->getSecurity()->encryptByKey($request->post('customerName'), $hash);
Decrypt
<? echo"Decode : ".Yii::$app->getSecurity()->decryptByKey($model->customerName, $model->customerHash);?>
Any help welcome.
Thanks

Related

Warning Code : 1681 UNSIGNED for decimal and floating point data types is deprecated and support for it will be removed in a future release

Today I've created a sql backup of a table using SQLyog Community Edition. Then I tried to run the created query against an up to date MySQL 8 managed database on Digital Ocean.
The query:
CREATE TABLE tblUsers (
id bigint unsigned NOT NULL AUTO_INCREMENT,
subscriberId bigint unsigned DEFAULT NULL,
user varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
password varchar(64) DEFAULT NULL,
created timestamp NULL DEFAULT NULL,
modified timestamp NULL DEFAULT NULL,
status varchar(16) DEFAULT NULL,
privilege varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
firstName varchar(64) DEFAULT NULL,
lastName varchar(64) DEFAULT NULL,
address1 varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
city varchar(32) DEFAULT NULL,
state char(2) DEFAULT NULL,
postalCode char(6) DEFAULT NULL,
phone varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
fax varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
email varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
notes varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
payRate varchar(16) DEFAULT NULL,
pay decimal(8,2) unsigned DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
The warning:
Warning Code : 1681
UNSIGNED for decimal and floating point data types is deprecated and support
for it will be removed in a future release.
The research:
https://dev.mysql.com/doc/refman/8.0/en/numeric-type-attributes.html
As of MySQL 8.0.17, AUTO_INCREMENT support is deprecated for FLOAT and
DOUBLE columns; you should expect it to be removed in a future version
of MySQL. Consider removing the AUTO_INCREMENT attribute from such
columns, or convert them to an integer type.
The question:
I'm not certain how to interpret what I'm reading. Is it really telling me to remove my primary key auto increments? Or is it telling me the pay field should no longer be define the way you see above? Or that AUTO_INCREMENT cannot exist together with unsigned?
This warning is not about your auto-increment column id. It's about the decimal column pay.
Change this:
pay decimal(8,2) unsigned DEFAULT NULL,
To this:
pay decimal(8,2) DEFAULT NULL,
The related excerpt from the manual page you linked to is the following. It tells you what to do if you want to restrict the column to nonnegative values:
As of MySQL 8.0.17, the UNSIGNED attribute is deprecated for columns of type FLOAT, DOUBLE, and DECIMAL (and any synonyms) and you should expect support for it to be removed in a future version of MySQL. Consider using a simple CHECK constraint instead for such columns.
In your case it would look like this:
pay decimal(8,2) DEFAULT NULL,
CHECK (pay >= 0),
Updated example:
id field remains unchanged
subscriberId field loses UNSIGNED and gains CHECK
pay field loses UNSIGNED and gains CHECK
CREATE TABLE tblUsers (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`subscriberId` BIGINT DEFAULT NULL CHECK (subscriberId >= 0),
`user` VARCHAR(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`password` VARCHAR(64) DEFAULT NULL,
`created` TIMESTAMP NULL DEFAULT NULL,
`modified` TIMESTAMP NULL DEFAULT NULL,
`status` VARCHAR(16) DEFAULT NULL,
`privilege` VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`firstName` VARCHAR(64) DEFAULT NULL,
`lastName` VARCHAR(64) DEFAULT NULL,
`address1` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`city` VARCHAR(32) DEFAULT NULL,
`state` CHAR(2) DEFAULT NULL,
`postalCode` CHAR(6) DEFAULT NULL,
`phone` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`fax` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`email` VARCHAR(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`notes` VARCHAR(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`payRate` VARCHAR(16) DEFAULT NULL,
`pay` DECIMAL(8,2) DEFAULT NULL CHECK (pay >= 0),
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=465000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

MySQL 1366 incorrect integer value

I am trying to import a csv file into a MySQL DB.
Said csv file is from a select * from the same table
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/import.csv'
INTO TABLE fisc_hist_header
character set utf8
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(-- a whole lot of fields ...)
SET `CREATED_DATE`=STR_TO_DATE( #yourdatecolumn, '%d/%m/%Y %H:%i:%s' )
The file starts with
545752715002093599;3;1;503955117000124560;28/11/2019 14:38:51;0 -- all the other fields
What I get is
After searching here, all results are for
Incorrect integer value: ''
What is different and strange to me is that for me, it gets the value but does not inputs it as an integer ?
EDIT : As asked here is the create table statement:
CREATE TABLE `fisc_hist_header` (
`fiscal_idx` bigint(20) NOT NULL AUTO_INCREMENT,
`invc_sid` bigint(20) NOT NULL,
`sbs_no` int(11) NOT NULL,
`store_no` int(5) NOT NULL,
`workstation_id` bigint(20) NOT NULL,
`created_date` datetime NOT NULL,
`invc_type` int(11) NOT NULL,
`cashier_name` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`associate_name` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`store_code` varchar(5) COLLATE utf8_bin DEFAULT NULL,
`store_name` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address1` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address2` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address3` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address4` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address5` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_address6` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`store_zip` varchar(10) COLLATE utf8_bin DEFAULT NULL,
`customer_sid` bigint(20) DEFAULT NULL,
`customer_title` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`customer_last_name` varchar(30) COLLATE utf8_bin DEFAULT NULL,
`customer_first_name` varchar(30) COLLATE utf8_bin DEFAULT NULL,
`customer_address1` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`customer_address2` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`customer_address3` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`customer_address4` varchar(40) COLLATE utf8_bin DEFAULT NULL,
`customer_zip_code` varchar(10) COLLATE utf8_bin DEFAULT NULL,
`customer_country` varchar(35) COLLATE utf8_bin DEFAULT NULL,
`customer_phone` varchar(30) COLLATE utf8_bin DEFAULT NULL,
`customer_email` varchar(60) COLLATE utf8_bin DEFAULT NULL,
`item_count` int(5) NOT NULL,
`grand_total_receipt` decimal(10,0) NOT NULL,
`prism_version` varchar(19) COLLATE utf8_bin DEFAULT NULL,
`plugin_version` varchar(19) COLLATE utf8_bin DEFAULT NULL,
`flag_first_record` char(1) COLLATE utf8_bin DEFAULT NULL,
`signature_key` varchar(500) COLLATE utf8_bin DEFAULT NULL,
`signature` varchar(500) COLLATE utf8_bin DEFAULT NULL,
`signature_previous` varchar(500) COLLATE utf8_bin DEFAULT NULL,
`signature_short` varchar(10) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`fiscal_idx`),
UNIQUE KEY `un_fhh` (`invc_sid`)
) ENGINE=InnoDB AUTO_INCREMENT=364 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As per the documentation for integer values, the maximum value for an unsigned integer which can be held in an INT column is 4294967295. For a standard signed integer it's 2147483647.
Your number 545752715002093599 is larger than both of these. You'll need to declare the target column as BIGINT in your fisc_hist_header table - the max value for a signed integer in this column type is 9223372036854775807.
These restrictions exist because of the number of bytes used by MySQL to store each value in the database. If you know you won't need to store values above a certain limit, you can use a smaller integer type in order to save disk space and memory.

I need to optimize a large data table in a mysql database

I have a table in a mysql DB with about 6 mil rows of data. Structure below. Most of my queries are searching for specific "customer" fields and display a value for each customer according to the value in column "value". The query searches the whole Table to match those customers specified in the query. This table started rather small but now it's gotten too big and my queries are taking quite some time to retrieve results. My questions is the following: If i create a separate table with just the customer field, along with an index, will that make my customer queries faster?
TABLE `data` (
`id` bigint(20) UNSIGNED NOT NULL,
`entry_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date` date DEFAULT NULL,
`media_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`media_type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`rate` decimal(8,2) DEFAULT NUCREATELL,
`value` decimal(8,2) DEFAULT NULL,
`page` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`sector` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`category` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`customer` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`product` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`image_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`supplier` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`time` time DEFAULT NULL,
`duration` time DEFAULT NULL,
`promoted_on` datetime NOT NULL,
`hasimage` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You want an index.
If you are searching for customers using in or = (the most common methods), then you can use a standard index on customer.
If you have more complex searches -- say using like with a leading wildcard -- then this does not work. A full text index might help. Or it might not, depending on the nature of the query and the data.
The "separate table" you're thinking about should be an index on your main table.
CREATE INDEX index_name
ON data(customer,value);
This will speed up the queries, and even prevent access to the table itself, at the cost of slightly slower INSERT and UPDATE operations.

MySQL Update Issue - NULL fields get updated, but not fields already containing data

I am struggling with a MySQL query. Here's the problem:
SELECT * from sf_forecasts WHERE contentid='1234' AND fulldate='2012-12-13'
There is one record in my table that matches this criteria, and this query finds it with no difficulty. However, when I move to update it using this query...
UPDATE sf_forecasts
SET listingTitle ='Some Snow Resort', location ='49.23, -115.22'
WHERE contentid='1234' AND fulldate='2012-12-13'
...I always get a notice of "0 rows affected." Strangely, the query is actually updating the record, but it's only touching fields that are NULL. If my example record has a 'location' value of NULL, the query will update the data, but if it already contains a non-NULL value, the query will not change it, even though the query clearly contains new information.
What gives?
P.S. I am using a PRIMARY key of contentid+fulldate (b-tree, unique) if this matters.
P.S.S. Here is the 'SHOW CREATE TABLE sf_forecasts' results as requested below:
sf_forecasts CREATE TABLE `sf_forecasts` (
`contentid` int(11) NOT NULL,
`fulldate` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`listingTitle` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`location` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`dayofmonth` smallint(2) NOT NULL,
`dayofweek` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`minTempF` smallint(4) NOT NULL,
`maxTempF` smallint(4) NOT NULL,
`minTempC` smallint(4) NOT NULL,
`maxTempC` smallint(4) NOT NULL,
`skyCondition` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`precip` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`snowPotential` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`wind` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`minTempF_custom` smallint(4) DEFAULT NULL,
`maxTempF_custom` smallint(4) DEFAULT NULL,
`minTempC_custom` smallint(4) DEFAULT NULL,
`maxTempC_custom` smallint(4) DEFAULT NULL,
`baseH_custom` smallint(4) DEFAULT NULL,
`baseL_custom` smallint(4) DEFAULT NULL,
`topH_custom` smallint(4) DEFAULT NULL,
`topL_custom` smallint(4) DEFAULT NULL,
`skyCondition_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`precip_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`snowPotential_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`wind_custom` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`icon` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`icon_custom` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`shortrange_custom` varchar(5000) COLLATE utf8_unicode_ci DEFAULT NULL,
`longrange_custom` varchar(5000) COLLATE utf8_unicode_ci DEFAULT NULL,
`scCode` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`scUpdate` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`lastUpdate` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`skys` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`contentid`,`fulldate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
The issue is with difference in datatype length defined and length of data being updated
Your location is varchar(10) and you are trying to insert 14 characters
You can always check if there are errors after query execution:
SHOW ERRORS;
SHOW WARNINGS;

Duplicate entry for key 'PRIMARY'

I'm trying to import a csv (that is a data extract from a SQL Server db) into MySQL.
I'm using this command to load the file:
LOAD DATA INFILE '/Users/Tyler/Desktop/playersToTeams.txt' INTO TABLE players_to_teams FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
And I get this error:
Error Code: 1062. Duplicate entry '124547' for key 'PRIMARY'
When I run: grep 124547 playersToTeams.txt:
119683,True,True,124547,1493,2011-08-31 02:22:56.630000000,,,,,http://bucknellbison.cstv.com///sports/m-wrestl/mtt/stolfi_joe00.html,,,,,,
124547,True,True,129411,14726,2011-08-31 02:22:56.630000000,Free/Breast,,,,http://usctrojans.collegesports.com/sports/m-swim/mtt/walling_emmett00.html,,,,,,
I can see that the 4th column of the first entry has the same number as the first column (pk, id), but the 4th column doesn't have any sort of index on it.
Here's the create schema created by sql workbench:
CREATE TABLE `players_to_teams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`IsActive` tinyint(1) DEFAULT NULL,
`IsVisible` tinyint(1) DEFAULT NULL,
`PlayerId` int(11) DEFAULT NULL,
`TeamId` int(11) DEFAULT NULL,
`CreationDate` datetime DEFAULT NULL,
`Position` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Club` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BT` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BioLink` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`OtherBioLink` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`StartYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`EndYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`NeulionPlayerID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`SeasonYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`GamesPlayed` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124549 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$
If I rerun the LOAD DATA command, without changing anything, I get a different error, this time the number is 2 more than the previous time (124549 vs. 124547). Running it again skips to 124552.
Any ideas what is causing this duplicate error?
Thanks
The file was ill-formatted. Props to #Vapire for thinking of that.