简体   繁体   English

我是否需要 WordPress 语句(查询)中的数组 ('%s', '%s') 或已过时?

[英]Do I need the array('%s', '%s') in a WordPress statement (query) or is obsolete?

Please forgive my ignorance, I've been learning how to develop WordPress plugins and I'm dabbling in database queries.请原谅我的无知,我一直在学习如何开发 WordPress 插件,并且正在涉足数据库查询。

Meanwhile, I was developing one of these I found this syntax for a query, actually the same concept twice, written in two different ways:同时,我正在开发其中一个我发现查询的语法,实际上是相同的概念两次,以两种不同的方式编写:

$wpdb->insert(
    $table_name,
    array(
        'name' => $name,
        'lat'  => $lat,
        'lng'  => $lng,
        array('%s', '%s')
);

$wpdb->update(
    $table_name,
    array(
        'name' => $name,
        'lat'  => $lat,
        'lng'  => $lng,
    array('%s'),
    array('%s')
);

First thing first, my question is if I need the array( '%s' ) to specify that is a string (is a sort of binding the value to prevent injection?) or can I skip it.首先,我的问题是我是否需要数组( '%s' )来指定它是一个字符串(是一种绑定值以防止注入?)或者我可以跳过它。 And secondly, is there any difference between the two queries?其次,这两个查询之间有什么区别吗? Is the second one wrong?第二个错了吗?

Thank you in advance.先感谢您。

First thing first, my question is if I need the array( '%s' ) to specify that is a string (is a sort of binding the value to prevent injection?) or can I skip it.首先,我的问题是我是否需要数组( '%s' )来指定它是一个字符串(是一种绑定值以防止注入?)或者我可以跳过它。

You can avoid format array(array( '%s' )) in both syntax.您可以避免在两种语法中格式化 array(array( '%s' )) 。 It is an optional parameter for query.它是查询的可选参数。 You can find it from below links:您可以从以下链接中找到它:

1) WPDB Insert 1) WPDB 插入

2) WPDB Update 2) WPDB 更新

And secondly, is there any difference between the two queries?其次,这两个查询之间有什么区别吗? Is the second one wrong?第二个错了吗?

For your second question, your first query inserts data into database & second updates data for all rows in database.对于您的第二个问题,您的第一个查询将数据插入数据库,第二个更新数据库中所有行的数据。 Here you can avoid where parameter if you want to update all data of your table.如果要更新表的所有数据,可以在此处避免 where 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM