Wordpress - $wpdb->insert - MySQL NOW()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8566603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:51:23  来源:igfitidea点击:

Wordpress - $wpdb->insert - MySQL NOW()

wordpress

提问by chris

is there any possibility to use the MySQL NOW() in the $wpdb->insert call?

是否有可能在 $wpdb->insert 调用中使用 MySQL NOW()?

When I use the following code, NOW() is not working.

当我使用以下代码时,NOW() 不起作用。

$data = array(
        'id' => NULL,
        'order' => serialize($_POST['data']['Order']),
        'created' => NOW(),
        'user_id' => $current_user->ID
    );

$wpdb->insert(ORDERS_TABLE, (array) $data );

回答by coderabbi

I believe the canonical approach is to use the WordPress current_time()function passing it 'mysql' as the first parameter to specify a mysql timestamp compatible format (the alternative is UNIX timestamp format) and '1' as the second parameter to specify local time (default is GMT), like this:

我相信规范的方法是使用 WordPresscurrent_time()函数传递它 'mysql' 作为第一个参数来指定 mysql 时间戳兼容格式(替代方法是 UNIX 时间戳格式)和 '1' 作为第二个参数来指定本地时间(默认是格林威治标准时间),像这样:

$data = array(
    'id' => NULL,
    'order' => serialize($_POST['data']['Order']),
    'created' => current_time('mysql', 1),
    'user_id' => $current_user->ID
);

$wpdb->insert(ORDERS_TABLE, $data);

current_time('mysql', 1)outputs 2012-07-18 12:51:13.

current_time('mysql', 1)输出2012-07-18 12:51:13

More here: http://codex.wordpress.org/Function_Reference/current_time

更多信息:http: //codex.wordpress.org/Function_Reference/current_time

回答by user2808180

As word "created" means that you only need the "NOW()", current date and time, on insert. You can alter the field created

由于“创建”一词意味着您在插入时只需要“NOW()”、当前日期和时间。您可以更改创建的字段

 ALTER TABLE `ORDERS_TABLE` CHANGE `created` `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

and don't use created field at all in query, so your new query will look like..

并且根本不要在查询中使用创建的字段,因此您的新查询将看起来像..

$data = array(        
    'order' => serialize($_POST['data']['Order']),        
    'user_id' => $current_user->ID
);

$wpdb->insert(ORDERS_TABLE, (array) $data );

and when you run your query "created" will take default value, it will be equal to NOW(), you may have noticed that I have omitted "id" as well it will also get its default value, I assume that it is auto incremented field.

并且当您运行查询“created”将采用默认值时,它将等于 NOW(),您可能已经注意到我省略了“id”以及它也将获得其默认值,我假设它是 auto增量字段。

回答by gbones

For anyone using Wordpress 5.3 and above, the recommended approachis to now use wp_date, not current_time.

对于使用 Wordpress 5.3 及更高版本的任何人,推荐的方法是现在使用wp_date而不是 current_time。

wp_date('Y-m-d H:i:s');

回答by paperclip

At present this is still not clear that you can pass these SQL functions like NOW() easily using the $wpdb->insert()method within the WordPress $wpdbclass.

目前还不清楚您是否可以使用$wpdb->insert()WordPress$wpdb类中的方法轻松传递这些 SQL 函数,如 NOW() 。

Short of writing a class to extend the $wpdbclass, the most straightforward way I can see is to use the $wpdb->query()method and write some code to convert your $dataarray to an SQL string to pass to the method.

除了编写类来扩展$wpdb类之外,我能看到的最直接的方法是使用该$wpdb->query()方法并编写一些代码将$data数组转换为 SQL 字符串以传递给该方法。

$sql = sprintf( 
    'INSERT INTO table (id, order, created, user_id) VALUES (%d, %d, %s, %d)',
    $data[id], $data['order'], $data['created'], $data['user_id'] 
);

$wpdb->update( $sql );

More here: http://codex.wordpress.org/Class_Reference/wpdb

更多信息:http: //codex.wordpress.org/Class_Reference/wpdb

回答by Cyto

It's an old topic, but I found a silly solution for using NOW() outside SQL statements and it works:

这是一个古老的话题,但我找到了一个在 SQL 语句之外使用 NOW() 的愚蠢解决方案,它有效:

$mysql_now = $wpdb->get_row( "SELECT NOW() as dbNow", ARRAY_A );
echo $mysql_now['dbNow'];

You are welcome.

不客气。