how to execute different sql query in non-sanitized $wpdb->get_results function

if $wpdb->get_results is not sanitized by core code, why we can’t execute different SQL query in it? Because the wpdb class uses (see wpdb::_do_query() ) mysqli_query() (by default) or mysql_query(), and unfortunately these functions do not support multiple queries such as SELECT <query>; INSERT <query>; SELECT <query>. Therefore, if you want to execute multiple SQL … Read more

How to pass NULL in where array for $wpdb->update

My simple and quick solution is the use of a normal $wpdb->query() function: $wpdb->query( $wpdb->prepare(“UPDATE $schedule_table SET rpt = %s WHERE post_id = %d AND start IS NULL AND end IS NULL”, $event_tag, $post_ID ) );

Use $wpdb in wordpress theme files

You need to connect your script to WordPress before you can use $wpdb. The usual way to do this is to include wp-blog-header.php: include “/path/to/wordpress/wp-blog-header.php”;

$wpdb->prepare not working with update table prefix

Your problem is likely that $wpdb->jch_gigs is undefined. Is jch_ the prefix of your DB tables, as defined in wp-config.php? If so, try this: $wpdb->prepare( “UPDATE {$wpdb->prefix}gigs SET available = available – %d WHERE ID = %d”, $quantity, $item ) ); wpdb class on the Codex

How-To: wpdb Insert Record With Date

date does not take a human readable string. It takes a Unix timestamp. You need to convert that human date to a timestamp with strtotime. ‘begin_date’ => date(‘Y-m-d’, strtotime(‘2012-08-14’)), Of course, there is really no need to convert a YEAR-MONTH-DAY date into a timestamp only to convert it back that a YEAR-MONTH-DAY format.

$wpdb prepare issue with mysql DATE_FORMAT

From the Codex page for the WPDB class: […] the prepare method […] supports both a sprintf()-like and vsprintf()-like syntax. Having a look at PHP’s documentation for sprintf(): ##Example 6 … // notice the double %%, this prints a literal ‘%’ character So you can use $result = $wpdb->get_var( $wpdb->prepare( “SELECT DATE_FORMAT(report_date, ‘%%d-%%m-%%Y’) FROM table … Read more

Set media metadata (i.e. “dimensions” field) on SVG file after extracting it with a filter

The wp_update_attachment_metadata filter fires every time you upload an image through media library (specifically every time wp_update_attachment_metadata function is being called). So when an svg file is uploaded we check if the file has the desired metadata, the width and height. If not, we generate the metadata as you did in your wp_get_attachment_image_src filter. Now … Read more

How do you use prepare when asking for a list of id’s

$wpdb->prepare() use the same syntax for formatting like php’s printf(). SSo you will need something like … WHERE post_id IN (%d,%d,%d) … This will fit for three ids. But what if we got more or less than three ids? We will use the php tools to create the needed formatting string (assuming $ids is an … Read more

WordPress insert NOW() in TIMESTAMP column returns all zeros

I hadn’t tried to reproduce it, but the likely reason is that what you want to use in query is SQL function (NOW()) but what you “tell” WordPress to use is a string. Declaring its format to be %s means it is sanitized and put in quotes, making it something like ‘NOW()’ which MySQL probably … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)