WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

Add support for `INSERT INTO ... VALUES ...` syntax in SQL queries

Open Chouby opened this issue 1 year ago • 1 comments
trafficstars

Bug Description

Prepared SQL queries with INSERT INTO ... VALUES ... filled with the correct number of placeholders using the syntax commonly used for queries with IN(...) clauses (implode() + array_fill()) emit a warning.

Minimal Code Snippet

The issue happens when running this command:

phpcs --standard=WordPress -s testfile.php

... over a file containing this code:

$all_relationships = array(
	array( 1, 1 ),
	array( 1, 2 ),
	array( 1, 3 ),
);
$wpdb->query(
	$wpdb->prepare(
		sprintf(
			"INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id) VALUES %s",
			implode( ',', array_fill( 0, count( $all_relationships ), '(%d,%d)' ) )
		),
		array_merge( ...$all_relationships )
	)
);

Error Code

Incorrect number of replacements passed to $wpdb->prepare().
Found 1 replacement parameters, expected 0.
(WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber)

Environment

Question Answer
PHP version 8.0.28
PHP_CodeSniffer version 3.10.3
WordPressCS version 3.1.0
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type Composer project local

Additional Context (optional)

Tested Against develop Branch?

  • [ ] I have verified the issue still exists in the develop branch of WordPressCS.

Chouby avatar Oct 02 '24 16:10 Chouby

Isn't this syntax deprecated https://dev.mysql.com/worklog/task/?id=13325 ?

kkmuffme avatar Jan 13 '25 09:01 kkmuffme