WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/24/2012 03:24:31 PM (8 years ago)
Author:
nacin
Message:

Introduce $wpdb->delete(). props justindgivens, scribu. fixes #18948.

File:
1 edited

Legend:

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

    r19773 r20287  
    405405     *
    406406     * @since 2.8.0
    407      * @see wpdb:prepare()
    408      * @see wpdb:insert()
    409      * @see wpdb:update()
     407     * @see wpdb::prepare()
     408     * @see wpdb::insert()
     409     * @see wpdb::update()
     410     * @see wpdb::delete()
    410411     * @see wp_set_wpdb_vars()
    411412     * @access public
     
    12711272        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    12721273    }
     1274
     1275    /**
     1276     * Delete a row in the table
     1277     *
     1278     * <code>
     1279     * wpdb::delete( 'table', array( 'ID' => 1 ) )
     1280     * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
     1281     * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ), 1 )
     1282     * </code>
     1283     *
     1284     * @since 2.5.0
     1285     * @see wpdb::prepare()
     1286     * @see wpdb::$field_types
     1287     * @see wp_set_wpdb_vars()
     1288     *
     1289     * @param string $table table name
     1290     * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
     1291     * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
     1292     * @return int|false The number of rows updated, or false on error.
     1293     */
     1294    function delete( $table, $where, $where_format = null ) {
     1295        if ( ! is_array( $where ) )
     1296            return false;
     1297
     1298        $bits = $wheres = array();
     1299
     1300        $where_formats = $where_format = (array) $where_format;
     1301
     1302        foreach ( array_keys( $where ) as $field ) {
     1303            if ( !empty( $where_format ) ) {
     1304                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1305            } elseif ( isset( $this->field_types[ $field ] ) ) {
     1306                $form = $this->field_types[ $field ];
     1307            } else {
     1308                $form = '%s';
     1309            }
     1310
     1311            $wheres[] = "$field = $form";
     1312        }
     1313
     1314        $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
     1315        return $this->query( $this->prepare( $sql, $where ) );
     1316    }
     1317
    12731318
    12741319    /**
Note: See TracChangeset for help on using the changeset viewer.