WordPress.org

Make WordPress Core

#41997 closed defect (bug) (duplicate)

Database error in SQL syntax ( $wpdb->prepare )

Reported by: Track77 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.2
Component: Database Keywords:
Focuses: Cc:

Description

I start receive next errors with new version.
This is an issue with function "$wpdb->prepare" on the line

<?php
$query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query );

Example code:

<?php
global $wpdb;
$results = $wpdb->get_results( 
$wpdb->prepare( '
        SELECT %1$s.ID FROM %1$s WHERE %1$s.post_type = "post"
        AND %1$s.post_status = "publish"
        LIMIT 1
', $wpdb->posts ), ARRAY_N );

var_dump( $results );

Result:

WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%1$s.ID FROM %1$s WHERE %1$s.post_type = "post"

	AND %1$s.post_status = "pu' at line 1 for query 

	SELECT %1$s.ID FROM %1$s WHERE %1$s.post_type = "post"

	AND %1$s.post_status = "publish"

	LIMIT 1

made by require_once('.../wp-admin/admin.php'), do_action('dashboard_page_test'), WP_Hook->do_action, WP_Hook->apply_filters, test_online, include('.../test.php')

Change History (1)

#1 @Clorith
22 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, and thanks for the report.

The use of numbered variables is actually an unsupported syntax, as noted in the documentation, the introduction of numbered variables are being discussed in #41925.

Note: See TracTickets for help on using tickets.