$wpdb->prepare()
接受单个参数数组作为第二个参数,例如。
$wpdb->prepare(
"SELECT * FROM table WHERE field1 = %s AND field2 = %d AND field3 = %s",
array( \'foo-bar\', 123, \'[email protected]\' )
)
因此,您可以将占位符数组与SQL命令(第一个参数)一起使用,如下所示:
// Create an array of placeholders, e.g. array( %s, %s, %s ) in your case.
$placeholders = implode( \', \', array_fill( 0, count( $the_array ), \'%s\' ) );
$db_options = $wpdb->get_col(
$wpdb->prepare(
// 1st parameter: The SQL command.
"SELECT option_name
FROM $wpdb->options
WHERE option_name NOT IN ($placeholders)",
// 2nd parameter: A single array of arguments.
$the_array
)
);
此外,请注意,如果传递一个数组,如
$the_array
则必须没有第3、4、5等参数;否则,将出现错误。因此,请传递单个参数或所有参数的单个数组,但决不能同时传递这两个参数!
// Good
$wpdb->prepare( "...", \'foo\', 123, \'etc.\' );
$wpdb->prepare( "...", array( \'foo\', 123, \'etc.\' ) );
// Bad - there is a 3rd parameter (\'etc.\')!
$wpdb->prepare( "...", array( \'foo\', 123 ), \'etc.\' );