| 1196 | 1196 | * |
| 1197 | 1197 | * The following placeholders can be used in the query string: |
| 1198 | 1198 | * %d (integer) |
| 1199 | 1199 | * %f (float) |
| 1200 | 1200 | * %s (string) |
| 1201 | 1201 | * |
| 1202 | 1202 | * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder. |
| 1203 | 1203 | * |
| 1204 | 1204 | * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, |
| 1205 | 1205 | * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these |
| 1206 | 1206 | * cannot be inserted directly in the query string. Also see {@see esc_like()}. |
| 1207 | 1207 | * |
| 1208 | 1208 | * This method DOES NOT support sign, padding, alignment, width or precision specifiers. |
| 1209 | 1209 | * This method DOES NOT support argument numbering or swapping. |
| 1210 | 1210 | * |
| 1212 | 1212 | * of the two is not supported. |
| 1213 | 1213 | * |
| 1214 | 1214 | * Examples: |
| 1215 | 1215 | * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); |
| 1216 | 1216 | * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
| 1217 | 1217 | * |
| 1218 | 1218 | * @link https://secure.php.net/sprintf Description of syntax. |
| 1219 | 1219 | * @since 2.3.0 |
| 1220 | 1220 | * |
| 1221 | 1221 | * @param string $query Query statement with sprintf()-like placeholders |
| 1222 | 1222 | * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called with an array of arguments, |
| 1223 | 1223 | * or the first variable to substitute into the query's placeholders if being called with individual arguments. |
| 1224 | 1224 | * @param mixed $args,... further variables to substitute into the query's placeholders if being called wih individual arguments. |
| 1225 | 1225 | * @return string|void Sanitized query string, if there is a query to prepare. |
| 1226 | 1226 | */ |
| 1775 | 1775 | |
| 1776 | 1776 | // MySQL server has gone away, try to reconnect. |
| 1777 | 1777 | $mysql_errno = 0; |
| 1778 | 1778 | if ( ! empty( $this->dbh ) ) { |
| 1779 | 1779 | if ( $this->use_mysqli ) { |
| 1780 | 1780 | if ( $this->dbh instanceof mysqli ) { |
| 1781 | 1781 | $mysql_errno = mysqli_errno( $this->dbh ); |
| 1782 | 1782 | } else { |
| 1783 | 1783 | // $dbh is defined, but isn't a real connection. |
| 1784 | 1784 | // Something has gone horribly wrong, let's try a reconnect. |
| 1785 | 1785 | $mysql_errno = 2006; |
| 1786 | 1786 | } |
| 1787 | 1787 | } else { |
| 1788 | 1788 | if ( is_resource( $this->dbh ) ) { |
| 1789 | 1789 | $mysql_errno = mysql_errno( $this->dbh ); |
| 1884 | | if ( ! empty( $this->dbh ) && $this->use_mysqli ) { |
| | 1884 | if ( ! empty( $this->dbh ) && $this->use_mysqli && $prepared_query_data !== false ) { |
| | 1885 | $prepared_value_types = ''; |
| | 1886 | $prepared_values = array(); |
| | 1887 | $valid_data_types = array( 's', 'd', 'i' ); |
| | 1888 | foreach ( $prepared_query_data as $v ) { |
| | 1889 | if ( is_array( $v ) && isset( $v['type'] ) ) { |
| | 1890 | if ( in_array( $v['type'], $valid_data_types, true ) ) { |
| | 1891 | $prepared_value_types .= $v['type']; |
| | 1892 | } else { |
| | 1893 | $prepared_value_types .= 's'; |
| | 1894 | } |
| | 1895 | $prepared_values[] = $v['value']; |
| | 1896 | } else { |
| | 1897 | // Strings can be passed without the data type. |
| | 1898 | $prepared_value_types .= 's'; |
| | 1899 | $prepared_values[] = $v; |
| | 1900 | } |
| | 1901 | } |
| | 1902 | |
| | 1903 | $prepared_query = mysqli_prepare( $this->dbh, $query ); |
| | 1904 | if ( ! $prepared_query ) { |
| | 1905 | // TODO: Handling of a invalid query |
| | 1906 | // $this->result = false; |
| | 1907 | } |
| | 1908 | |
| | 1909 | /*if ( $prepared_query->param_count != count( $__raw_prepared_data ) ) { |
| | 1910 | // Catch this before a PHP Warning is hit and yell even louder than a Warning at the developer? |
| | 1911 | _doing_it_completely_wrong( "Incorrect parameter count!" ); |
| | 1912 | throw new Exception( 'Incorrect parameter count!' ); |
| | 1913 | }*/ |
| | 1914 | |
| | 1915 | $mysqli_stmt_bind_param_args = array( |
| | 1916 | $prepared_query, |
| | 1917 | $prepared_value_types |
| | 1918 | // ... args by ref: |
| | 1919 | ); |
| | 1920 | foreach ( $prepared_values as $i => $v ) { |
| | 1921 | $mysqli_stmt_bind_param_args[] = & $prepared_values[$i]; |
| | 1922 | } |
| | 1923 | call_user_func_array( 'mysqli_stmt_bind_param', $mysqli_stmt_bind_param_args ); |
| | 1924 | |
| | 1925 | mysqli_stmt_execute( $prepared_query ); |
| | 1926 | |
| | 1927 | $this->result = mysqli_stmt_get_result( $prepared_query ); |
| | 1928 | } elseif ( ! empty( $this->dbh ) && $this->use_mysqli ) { |
| 1887 | 1936 | $this->result = mysql_query( $query, $this->dbh ); |
| 1888 | 1937 | } |
| 1889 | 1938 | $this->num_queries++; |
| 1890 | 1939 | |
| 1891 | 1940 | if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
| 1892 | 1941 | $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
| 1893 | 1942 | } |
| 1894 | 1943 | } |
| 1895 | 1944 | |
| 1896 | 1945 | /** |
| 1897 | 1946 | * Insert a row into a table. |
| 1898 | 1947 | * |
| 1899 | 1948 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
| 1900 | 1949 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
| 1901 | 1950 | * |
| 2052 | 2101 | } |
| 2053 | 2102 | foreach ( $where as $field => $value ) { |
| 2054 | 2103 | if ( is_null( $value['value'] ) ) { |
| 2055 | 2104 | $conditions[] = "`$field` IS NULL"; |
| 2056 | 2105 | continue; |
| 2057 | 2106 | } |
| 2058 | 2107 | |
| 2059 | 2108 | $conditions[] = "`$field` = " . $value['format']; |
| 2060 | 2109 | $values[] = $value['value']; |
| 2061 | 2110 | } |
| 2062 | 2111 | |
| 2063 | 2112 | $fields = implode( ', ', $fields ); |
| 2064 | 2113 | $conditions = implode( ' AND ', $conditions ); |
| 2065 | 2114 | |
| 2066 | 2115 | $sql = "UPDATE `$table` SET $fields WHERE $conditions"; |
| 2068 | 2117 | $this->check_current_query = false; |
| 2069 | 2118 | return $this->query( $this->prepare( $sql, $values ) ); |
| 2070 | 2119 | } |
| 2071 | 2120 | |
| 2072 | 2121 | /** |
| 2073 | 2122 | * Delete a row in the table |
| 2074 | 2123 | * |
| 2075 | 2124 | * wpdb::delete( 'table', array( 'ID' => 1 ) ) |
| 2076 | 2125 | * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
| 2077 | 2126 | * |
| 2078 | 2127 | * @since 3.4.0 |
| 2079 | 2128 | * @see wpdb::prepare() |
| 2080 | 2129 | * @see wpdb::$field_types |
| 2081 | 2130 | * @see wp_set_wpdb_vars() |
| 2082 | 2131 | * |
| 2260 | 2309 | |
| 2261 | 2310 | /** |
| 2262 | 2311 | * Retrieve one variable from the database. |
| 2263 | 2312 | * |
| 2264 | 2313 | * Executes a SQL query and returns the value from the SQL result. |
| 2265 | 2314 | * 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. |
| 2266 | 2315 | * If $query is null, this function returns the value in the specified column and row from the previous SQL result. |
| 2267 | 2316 | * |
| 2268 | 2317 | * @since 0.71 |
| 2269 | 2318 | * |
| 2270 | 2319 | * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. |
| 2271 | 2320 | * @param int $x Optional. Column of value to return. Indexed from 0. |
| 2272 | 2321 | * @param int $y Optional. Row of value to return. Indexed from 0. |
| 2273 | 2322 | * @return string|null Database query result (as string), or null on failure |
| 2274 | 2323 | */ |
| 2284 | 2344 | } |
| 2285 | 2345 | |
| 2286 | 2346 | // Extract var out of cached results based x,y vals |
| 2287 | 2347 | if ( !empty( $this->last_result[$y] ) ) { |
| 2288 | 2348 | $values = array_values( get_object_vars( $this->last_result[$y] ) ); |
| 2289 | 2349 | } |
| 2290 | 2350 | |
| 2291 | 2351 | // If there is a value return it else return null |
| 2292 | 2352 | return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; |
| 2293 | 2353 | } |
| 2294 | 2354 | |
| 2295 | 2355 | /** |
| 2296 | 2356 | * Retrieve one row from the database. |
| 2297 | 2357 | * |
| 2298 | 2358 | * Executes a SQL query and returns the row from the SQL result. |
| 2299 | 2359 | * |
| 2300 | 2360 | * @since 0.71 |
| 2301 | 2361 | * |
| 2302 | 2362 | * @param string|null $query SQL query. |
| 2303 | 2363 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
| 2304 | 2364 | * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. |
| 2305 | 2365 | * @param int $y Optional. Row to return. Indexed from 0. |
| 2306 | 2366 | * @return array|object|null|void Database query result in format specified by $output or null on failure |
| 2307 | 2367 | */ |
| 2317 | 2389 | } else { |
| 2318 | 2390 | return null; |
| 2319 | 2391 | } |
| 2320 | 2392 | |
| 2321 | 2393 | if ( !isset( $this->last_result[$y] ) ) |
| 2322 | 2394 | return null; |
| 2323 | 2395 | |
| 2324 | 2396 | if ( $output == OBJECT ) { |
| 2325 | 2397 | return $this->last_result[$y] ? $this->last_result[$y] : null; |
| 2326 | 2398 | } elseif ( $output == ARRAY_A ) { |
| 2327 | 2399 | return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; |
| 2328 | 2400 | } elseif ( $output == ARRAY_N ) { |
| 2329 | 2401 | return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; |
| 2330 | 2402 | } elseif ( strtoupper( $output ) === OBJECT ) { |
| 2331 | 2403 | // Back compat for OBJECT being previously case insensitive. |
| 2336 | 2408 | } |
| 2337 | 2409 | |
| 2338 | 2410 | /** |
| 2339 | 2411 | * Retrieve one column from the database. |
| 2340 | 2412 | * |
| 2341 | 2413 | * Executes a SQL query and returns the column from the SQL result. |
| 2342 | 2414 | * If the SQL result contains more than one column, this function returns the column specified. |
| 2343 | 2415 | * If $query is null, this function returns the specified column from the previous SQL result. |
| 2344 | 2416 | * |
| 2345 | 2417 | * @since 0.71 |
| 2346 | 2418 | * |
| 2347 | 2419 | * @param string|null $query Optional. SQL query. Defaults to previous query. |
| 2348 | 2420 | * @param int $x Optional. Column to return. Indexed from 0. |
| 2349 | 2421 | * @return array Database query result. Array indexed from 0 by SQL result row number. |
| 2350 | 2422 | */ |
| 2358 | 2436 | } |
| 2359 | 2437 | |
| 2360 | 2438 | $new_array = array(); |
| 2361 | 2439 | // Extract the column values |
| 2362 | 2440 | for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { |
| 2363 | 2441 | $new_array[$i] = $this->get_var( null, $x, $i ); |
| 2364 | 2442 | } |
| 2365 | 2443 | return $new_array; |
| 2366 | 2444 | } |
| 2367 | 2445 | |
| 2368 | 2446 | /** |
| 2369 | 2447 | * Retrieve an entire SQL result set from the database (i.e., many rows) |
| 2370 | 2448 | * |
| 2371 | 2449 | * Executes a SQL query and returns the entire SQL result. |
| 2372 | 2450 | * |
| 2373 | 2451 | * @since 0.71 |
| 2374 | 2452 | * |
| 2375 | 2453 | * @param string $query SQL query. |
| 2376 | 2454 | * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
| 2377 | 2455 | * With one of the first three, return an array of rows indexed from 0 by SQL result row number. |
| 2378 | 2456 | * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. |
| 2379 | 2457 | * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. |
| 2380 | 2458 | * Duplicate keys are discarded. |
| 2381 | 2459 | * @return array|object|null Database query results |
| 2382 | 2460 | */ |
| 2392 | 2476 | } else { |
| 2393 | 2477 | return null; |
| 2394 | 2478 | } |
| 2395 | 2479 | |
| 2396 | 2480 | $new_array = array(); |
| 2397 | 2481 | if ( $output == OBJECT ) { |
| 2398 | 2482 | // Return an integer-keyed array of row objects |
| 2399 | 2483 | return $this->last_result; |
| 2400 | 2484 | } elseif ( $output == OBJECT_K ) { |
| 2401 | 2485 | // Return an array of row objects with keys from column 1 |
| 2402 | 2486 | // (Duplicates are discarded) |
| 2403 | 2487 | foreach ( $this->last_result as $row ) { |
| 2404 | 2488 | $var_by_ref = get_object_vars( $row ); |
| 2405 | 2489 | $key = array_shift( $var_by_ref ); |
| 2406 | 2490 | if ( ! isset( $new_array[ $key ] ) ) |