aes_decryption don't work in phpmyadmin - mysql

I have one table in mysql database:
CREATE TABLE IF NOT EXISTS `t` (
`q` varchar(257) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I added two values to it - one is through mysql-console and other from phpmyadmin:
insert into t(q) values(aes_encrypt('from phpmyadmin', 123456));
insert into t(q) values(aes_encrypt('from mysql console', 123456));
And I tried to display it:
select aes_decrypt(q,123456) from t;
From mysql-console I got the following out put:
mysql> select aes_decrypt(q,123456) from t;
+-----------------------+
| aes_decrypt(q,123456) |
+-----------------------+
| from phpmyadmin |
| from mysql console |
+-----------------------+
2 rows in set (0.00 sec)
From phpadmin I got the following output:
why phpmyadmin don't show correct output?

aes_decrypt function produces binary data.
Try
select cast(aes_decrypt(q,123456) as char) from t LIMIT 0, 30;
on your phpMyAdmin.

You can try the following query
select aes_decrypt(unhex(q),123456) from t;

Related

SQL "SELECT" Command Not Working, even though entry is in DB

I have created the following database:
CREATE TABLE QuizRepo (
User_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
Name TEXT
)
I populate it via JDBC, and when I populate it, I get:
mysql> select * from QuizRepo;
| User_ID | Name |
| 1 | "XXQuiz"|
When I do the following command, it works as expected:
mysql> select * from QuizRepo where USER_ID=1;
User_ID | Name |
| 1 | "XXQuiz"|
However, when I do the following command, I get a weird result
mysql> select * from QuizRepo where Name="XXQuiz";
Empty set (0.01 sec)
Has anyone seen this happen before? How could this be possible? Perhaps I am adding it in the DB incorrectly (doesnt seem likely) but then you can clearly see there is an entry called "XXQuiz" so why is it not finding it?
It looks like you've stored the quotes as well. So you'll need to so a :
select * from QuizRepo where Name = "\"XXQuiz\"";
or similar.
Could be you name don't match exactly check for this and try also
select * from QuizRepo where Name like "%XXQuiz%";

MySQL SELECT * fails on temporary table created by PREPARE using dynamic column and table names after first pass

MySQL 5.2, CentOS 6.4.
MySQL SELECT * fails on temporary table created by PREPARE using dynamic column and table names after first pass, when column name and table name are changed to different values from the first pass.
The work-around is to use a column alias that remains the same from pass to pass.
DROP PROCEDURE IF EXISTS test1;
DELIMITER $$
CREATE PROCEDURE test1( column_name VARCHAR(20), table_name VARCHAR(20) )
BEGIN
SET #prepared_stmt_arg = 'prepared_stmt_arg_value';
DROP TABLE IF EXISTS tmp1;
CREATE TEMPORARY TABLE tmp1
SELECT 1 AS col_tmp1;
DROP TABLE IF EXISTS tmp2;
CREATE TEMPORARY TABLE tmp2
SELECT 2 AS col_tmp2;
# drop tmp table if it exists
DROP TABLE IF EXISTS tmp_test1;
# prepared statement
SET #prepared_stmt =
CONCAT("
CREATE TEMPORARY TABLE tmp_test1
SELECT ? AS prepared_stmt_arg, ", column_name, " # AS constant_col_alias
FROM ", table_name, "
"); # END statement
# display prepared statement before executing it
SELECT #prepared_stmt;
# prepare the statement
PREPARE ps FROM #prepared_stmt;
# execute
EXECUTE ps USING #prepared_stmt_arg;
# deallocate
DEALLOCATE PREPARE ps;
# display
SELECT * FROM tmp_test1;
END $$
DELIMITER ;
The SELECT statement at the very end of the procedure fails. (You may need to scroll down to see the error message.)
mysql> CALL test1('col_tmp1', 'tmp1');
+---------------------------------------------------------------------------------------------------------------------------------+
| #prepared_stmt |
+---------------------------------------------------------------------------------------------------------------------------------+
|
CREATE TEMPORARY TABLE tmp_test1
SELECT ? AS prepared_stmt_arg, col_tmp1 # AS constant_col_alias
FROM tmp1
|
+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------------------------+----------+
| prepared_stmt_arg | col_tmp1 |
+-------------------------+----------+
| prepared_stmt_arg_value | 1 |
+-------------------------+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> CALL test1('col_tmp2', 'tmp2');
+---------------------------------------------------------------------------------------------------------------------------------+
| #prepared_stmt |
+---------------------------------------------------------------------------------------------------------------------------------+
|
CREATE TEMPORARY TABLE tmp_test1
SELECT ? AS prepared_stmt_arg, col_tmp2 # AS constant_col_alias
FROM tmp2
|
+---------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
ERROR 1054 (42S22): Unknown column 'dev.tmp_test1.col_tmp1' in 'field list'
However, if you uncomment the column alias (remove the # just before AS constant_col_alias), all works well. (You may need to scroll down to see Query OK.)
mysql> CALL test1('col_tmp1', 'tmp1');
+-------------------------------------------------------------------------------------------------------------------------------+
| #prepared_stmt |
+-------------------------------------------------------------------------------------------------------------------------------+
|
CREATE TEMPORARY TABLE tmp_test1
SELECT ? AS prepared_stmt_arg, col_tmp1 AS constant_col_alias
FROM tmp1
|
+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------------------------+--------------------+
| prepared_stmt_arg | constant_col_alias |
+-------------------------+--------------------+
| prepared_stmt_arg_value | 1 |
+-------------------------+--------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> CALL test1('col_tmp2', 'tmp2');
+-------------------------------------------------------------------------------------------------------------------------------+
| #prepared_stmt |
+-------------------------------------------------------------------------------------------------------------------------------+
|
CREATE TEMPORARY TABLE tmp_test1
SELECT ? AS prepared_stmt_arg, col_tmp2 AS constant_col_alias
FROM tmp2
|
+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
+-------------------------+--------------------+
| prepared_stmt_arg | constant_col_alias |
+-------------------------+--------------------+
| prepared_stmt_arg_value | 2 |
+-------------------------+--------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Well it seems to be a bug or a feature (if you want) up to version 5.6.
See Bug #32868 Stored routines do not detect changes in meta-data.
Workaround: flush the stored routine cache by doing this:
CREATE OR REPLACE VIEW tmpview AS SELECT 1;
Here is SQLFiddle demo MySql 5.1.X
Here is SQLFiddle demo MySql 5.5.X
If you comment out CREATE OR REPLACE VIEWtmpviewAS SELECT 1 you'll get your error.
Here is SQLFiddle demo MySql 5.6.X shows that it's no longer a problem
Now you have at least these options to go with:
don't use SELECT * use explicit column names instead.
use proposed workaround
upgrade to 5.6.X

Trigger with "NEW" doesn't work

it's the first time for me with triggers on MySQL.
I've two different tables ('users' and 'prova') and I want to insert a new row in 'prova' every time there is a new row in the 'users' table.
CREATE TRIGGER inserisciemail
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO prova (provaemail)
VALUES (NEW.email);
END
The field 'provaemail' results empty and only the id field is filled (autoincremented).
What's wrong?
Vito
I'm moving our discussion here because SO suggests to avoid extended discussions in comments.
So, thank you for the SQLs but I'm afraid they didn't include the CREATE TABLE for "prova" and the INSERT statement running on "users" table.
Anyways, I created the "users" table and the trigger on my dev environment. Then I created my own version of "prova" table as below:
CREATE TABLE `prova` (
`provaemail` VARCHAR(40)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Then ran the below insert statement:
INSERT INTO `users` (`id`, `email`) VALUES (1, 'guptaabhay#gmail.com');
And a new entry was inserted to "prova", here:
mysql> select * from prova;
+----------------------+
| provaemail |
+----------------------+
| guptaabhay#gmail.com |
+----------------------+
1 row in set (0.00 sec)
So the trigger worked!
It would be great if you could share the schema for "prova" and your INSERT INTO users query so that we can research further. I'm sure something's amiss.
EDIT 1
Thank you for the INSERTs. They ran fine and the "prova" table has now the following entries:
mysql> select * from prova;
+-----------------------+
| provaemail |
+-----------------------+
| genoveffa#dominio.it |
| peto#dominio.it |
| test#dominio.it |
| gianni#dominio.it |
| nuovissimo#dominio.it |
| new#dominio.it |
| vit#dominio.it |
+-----------------------+
8 rows in set (0.00 sec)
So nothing weird till now! Why don't you try these steps once at your end:
create "users" table
create trigger
create "prova" table (using the CREATE statement I've given above)
fire the inserts as given on http://pastie.org/3166828
do SELECT * FROM prova;
And let me know whatever you see?

2 servers, 2 memory tables, different sizes

I have got two servers both running a MySQL instance. The first one, server1, is running MySQL 5.0.22. The other one, server2, is running MySQL 5.1.58.
When I create a memory table on server1 and I add a row its size is instantly 8,190.0 KiB.
When I create a memory table on server2 and I add a row its size is still only some bytes, though.
Is this caused by the difference in MySQL version or (hopefully) is this due to some setting I can change?
EDIT:
I haven't found the reason for this behaviour yet, but I did found a workaround. So, for future references, this is what fixed it for me:
All my memory tables are made once and are read-only from thereon. When you specify to MySQL the maximum number of rows your table will have, its size will shrink. The following query will do that for you.
ALTER TABLE table_name MAX_ROWS = N
Factor of 2?
OK, the problem likely is caused by the UTF-8 vs latin1
:- http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
You can check the database connection, database default character set for both servers.
here is the testing I have just done :-
mysql> create table test ( name varchar(10) ) engine
-> =memory;
Query OK, 0 rows affected (0.03 sec)
mysql> show create table test;
+-------+------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`name` varchar(10) DEFAULT NULL
) ENGINE=MEMORY DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into test values ( 1 );
mysql> set names utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> create table test2 ( name varchar(10) ) engine =memory default charset = utf8;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test2 values ( convert(1 using utf8) );
Query OK, 1 row affected (0.01 sec)
mysql> select table_name, avg_row_length from information_schema.tables where TABLE_NAME in( 'test2', 'test');
+------------+----------------+
| table_name | avg_row_length |
+------------+----------------+
| test | 12 |
| test2 | 32 |
+------------+----------------+
2 rows in set (0.01 sec)

Attaching simple metadata to a MySQL database

Is there a way to attach a piece of metadata to a MySQL database? I'm trying to write code to automatically update the database schema whenever a code upgrade requires it. This requires the storage of a single integer value -- the schema version. I could of course create a whole table for it, but that seems like overkill for just a simple number.
You can use table comments to store the version:
ALTER TABLE table1 COMMENT = '1.4';
You'll have to regex to get the comment from this:
SHOW CREATE TABLE table1;
/COMMENT='(.*)'/
To answer the question as titled, that is for metadata for the entire database and not individual tables, there are a couple of choices, depending on the privileges that you have.
The most direct route is to create a stored function, which requires the CREATE ROUTINE privilege. e.g.
mysql> CREATE FUNCTION `mydb`.DB_VERSION() RETURNS VARCHAR(15)
RETURN '1.2.7.2861';
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT `mydb`.DB_VERSION();
+--------------+
| DB_VERSION() |
+--------------+
| 1.2.7.2861 |
+--------------+
1 row in set (0.06 sec)
If your privileges limit you to only creating tables, you can create a simple table and put the metadata as default values. There’s no need to store any data in the table.
mysql> CREATE TABLE `mydb`.`db_metadata` (
`version` varchar(15) not null default '1.2.7.2861');
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW COLUMNS FROM `mydb`.`db_metadata`;
+---------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+------------+-------+
| version | varchar(15) | NO | | 1.2.7.2861 | |
+---------+-------------+------+-----+------------+-------+
1 row in set (0.00 sec)