+ /**
+ * Replace a row into a table.
+ *
+ * <code>
+ * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
+ * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
+ * </code>
+ *
+ * @since 3.0.0
+ * @see wpdb::prepare()
+ * @see wpdb::$field_types
+ * @see wp_set_wpdb_vars()
+ *
+ * @param string $table table name
+ * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
+ * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
+ * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
+ * @return int|false The number of rows affected, or false on error.
+ */
+ function replace( $table, $data, $format = null ) {
+ return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
+ }
+
+ /**
+ * Helper function for insert and replace.
+ *
+ * Runs an insert or replace query based on $type argument.
+ *
+ * @access private
+ * @since 3.0.0
+ * @see wpdb::prepare()
+ * @see wpdb::$field_types
+ * @see wp_set_wpdb_vars()
+ *
+ * @param string $table table name
+ * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
+ * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data.
+ * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
+ * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
+ * @return int|false The number of rows affected, or false on error.
+ */
+ function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
+ if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
+ return false;
+ $this->insert_id = 0;
+ $formats = $format = (array) $format;
+ $fields = array_keys( $data );
+ $formatted_fields = array();
+ foreach ( $fields as $field ) {
+ if ( !empty( $format ) )
+ $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
+ elseif ( isset( $this->field_types[$field] ) )
+ $form = $this->field_types[$field];
+ else
+ $form = '%s';
+ $formatted_fields[] = $form;
+ }
+ $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
+ return $this->query( $this->prepare( $sql, $data ) );
+ }
+
+ /**
+ * Update a row in the table
+ *
+ * <code>
+ * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
+ * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
+ * </code>
+ *
+ * @since 2.5.0
+ * @see wpdb::prepare()
+ * @see wpdb::$field_types
+ * @see wp_set_wpdb_vars()
+ *
+ * @param string $table table name
+ * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
+ * @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".
+ * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data.
+ * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
+ * @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.
+ * @return int|false The number of rows updated, or false on error.
+ */
+ function update( $table, $data, $where, $format = null, $where_format = null ) {
+ if ( ! is_array( $data ) || ! is_array( $where ) )
+ return false;
+
+ $formats = $format = (array) $format;
+ $bits = $wheres = array();
+ foreach ( (array) array_keys( $data ) as $field ) {
+ if ( !empty( $format ) )
+ $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
+ elseif ( isset($this->field_types[$field]) )
+ $form = $this->field_types[$field];
+ else
+ $form = '%s';
+ $bits[] = "`$field` = {$form}";
+ }
+
+ $where_formats = $where_format = (array) $where_format;
+ foreach ( (array) array_keys( $where ) as $field ) {
+ if ( !empty( $where_format ) )
+ $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
+ elseif ( isset( $this->field_types[$field] ) )
+ $form = $this->field_types[$field];
+ else
+ $form = '%s';
+ $wheres[] = "`$field` = {$form}";
+ }
+
+ $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
+ return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
+ }
+
+ /**
+ * Delete a row in the table
+ *
+ * <code>
+ * wpdb::delete( 'table', array( 'ID' => 1 ) )
+ * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
+ * </code>
+ *
+ * @since 3.4.0
+ * @see wpdb::prepare()
+ * @see wpdb::$field_types
+ * @see wp_set_wpdb_vars()
+ *
+ * @param string $table table name
+ * @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".
+ * @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.
+ * @return int|false The number of rows updated, or false on error.
+ */
+ function delete( $table, $where, $where_format = null ) {
+ if ( ! is_array( $where ) )
+ return false;
+
+ $bits = $wheres = array();
+
+ $where_formats = $where_format = (array) $where_format;
+
+ foreach ( array_keys( $where ) as $field ) {
+ if ( !empty( $where_format ) ) {
+ $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
+ } elseif ( isset( $this->field_types[ $field ] ) ) {
+ $form = $this->field_types[ $field ];
+ } else {
+ $form = '%s';
+ }
+
+ $wheres[] = "$field = $form";
+ }
+
+ $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
+ return $this->query( $this->prepare( $sql, $where ) );
+ }
+
+
+ /**
+ * Retrieve one variable from the database.
+ *
+ * Executes a SQL query and returns the value from the SQL result.
+ * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified.
+ * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
+ *
+ * @since 0.71
+ *
+ * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
+ * @param int $x Optional. Column of value to return. Indexed from 0.
+ * @param int $y Optional. Row of value to return. Indexed from 0.
+ * @return string|null Database query result (as string), or null on failure
+ */
+ function get_var( $query = null, $x = 0, $y = 0 ) {
+ $this->func_call = "\$db->get_var(\"$query\", $x, $y)";