Welcome Guest, Not a member yet? Register   Sign In
CURRENT_TIMESTAMP: different mysql-behavior phpmyadmin vs. CI
#1

(This post was last modified: 11-29-2018, 05:32 AM by kbs170.)

Hi,

We have a problem with different behavior between executing mysql-query in phpmyadmin and Codeigniter. In the same UPDATE-Query we would like do both save an old timestamp (means: "move" to an other field)  and set the new timestamp. That doesn't work with Codeigniter. Codeigniter sets both fields with the new value.

Why that?

One thing we recognize: It works with Codeigniter, If we stop using the mysql-function "ON UPDATE CURRENT_TIMESTAMP". Actual "ON UPDATE CURRENT_TIMESTAMP" is a "core-function" of our system, because we do not implement a modified=NOW() statement at all.

Code:
CREATE TABLE `test` (
  `id` int(6) NOT NULL,
  `name` char(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified2` datetime DEFAULT NULL,
  `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `test` (`id`, `name`, `created`, `modified2`, `modified`) VALUES
(1, 'Test1', NULL, NULL, '2018-11-29 11:50:39'),
(2, 'Test2', NULL, NULL, '2018-11-29 11:50:39');


in phpmyadmin
Code:
UPDATE
  test
SET
  modified2 = modified,
  modified = NOW()
WHERE
  id = 1

results (as expected):
(1, 'Test1', NULL, '2018-11-29 12:50:39', '2018-11-29 11:52:44')

in Codeigniter

PHP Code:
$this->db->query('
UPDATE
  test
SET
  modified2 = modified,
  modified = NOW()
WHERE
  id = 1
'
); 

results (not expected):
(1, 'Test1', NULL, '2018-11-29 11:52:44', '2018-11-29 11:52:44')

It should
  1. set "modified2" with the old value of "modified" (2018-11-29 12:50:39)
  2. set "modified" with the new value
How could that be?

Best,
kbs170
Reply
#2

If you are keeping your queries, you can do
PHP Code:
echo $this->db->last_query(); 

That should give you exact SQL query CI was trying to execute.
Reply
#3

(11-29-2018, 06:09 AM)Pertti Wrote: If you are keeping your queries, you can do
PHP Code:
echo $this->db->last_query(); 

That should give you exact SQL query CI was trying to execute.

That gives:

Code:
UPDATE test SET modified2 = modified, modified = NOW() WHERE id = 1

as well. Unfortunately no new insight. Thank you for the hint.

Best
Reply
#4

Does it work if you split it in two different queries, one for modified2 = modified and another for modified = NOW()?
Reply
#5

(This post was last modified: 11-29-2018, 08:41 AM by kbs170.)

I found the mistake. There is a bundle of UPDATEs before that. All without modified=NOW() but - of course - mysql set "modified" automatically (because of ON UPDATE CURRENT_TIMESTAMP).

Sorry, and thank you for your support.

Best,
Reply
#6

(This post was last modified: 11-29-2018, 10:07 AM by InsiteFX.)

This is the correct way of doing it.

Code:
 `created_at` DATETIME  DEFAULT CURRENT_TIMESTAMP,
 `updated_at` DATETIME  DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

Works with DATETIME and TIMESTAMP

Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB