Make WordPress Core


Ignore:
Timestamp:
10/02/2017 02:10:14 AM (8 years ago)
Author:
pento
Message:

Database: Throw a notice if wpdb::prepare() is called with an incorrect number of arguments

wpdb::prepare() currently gives no information if the number of arguments passed doesn't match the number of placeholders in the query. This change gives an explicit notice that the call was incorrect.

Also fixes an enrelated term meta test that was triggering this new notice.

Props thekt12 for the initial patch.
Fixes #42040.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r41660 r41662  
    12521252        $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
    12531253        $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
    1254         $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
     1254        $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
     1255
     1256        // Count the number of valid placeholders in the query
     1257        $placeholders = preg_match_all( '/(^|[^%]|(%%)+)%[sdF]/', $query );
     1258
     1259        if ( count ( $args ) !== $placeholders ) {
     1260            _doing_it_wrong( 'wpdb::prepare',
     1261                sprintf( __( 'The query does not contain the correct number of placeholders (%d) for the number of arguments passed (%d).' ),
     1262                    $placeholders,
     1263                    count( $args ) ),
     1264                '4.9.0'
     1265            );
     1266        }
     1267
    12551268        array_walk( $args, array( $this, 'escape_by_ref' ) );
    12561269        return @vsprintf( $query, $args );
     
    20472060
    20482061        $sql = "UPDATE `$table` SET $fields WHERE $conditions";
    2049 
     2062       
    20502063        $this->check_current_query = false;
    20512064        return $this->query( $this->prepare( $sql, $values ) );
Note: See TracChangeset for help on using the changeset viewer.