WPDB Placeholders and second argument for prepared statements

Just use %s for string and %d for digit replacements. Do not forget to use the proper esc_*() functions (use full text search for esc_ in the function reference). global $wpdb; $wpdb->show_errors = true; $wpdb->suppress_errors = false; ! defined( ‘DIEONDBERROR’ ) and define( ‘DIEONDBERROR’, true ); $sql = <<<SQL SELECT ID FROM {$wpdb->posts} WHERE post_type=”attachment” … Read more

AWS Bitnami WordPress – SELECT command denied to user

This is something that has thrown me off in the past. You actually see the error that talking about the Privileges, but the actual error is that the database doesn’t exist! You are searching for database name wp_ where there is only bitnami_wordpress. $sql = “SELECT * FROM bitnami_wordpress.wp_comments WHERE comment_ID = $id”;

$wpdb->insert inserting only f character in custom table

$_POST[‘param’] is a string, you can not treat it like an array. That’s why only f characters are inserted. You have two options, prepare data as an array in javascript or parse the string to array on the PHP side. Sample code for 2nd option: $table = $wpdb->prefix.’newsletter’; // ‘firstname=FIRSTNAME&lastname=LASTNAME&email=EMAIL%40DOMAIN.COM&type=TYPE’ $input = explode( ‘&’, $_POST[‘param’] … Read more

Can’t pass variable in wordpress wpdb->get_results

As you can see in the Codex pages the get_results doesn’t provide the functionality to add a value parameter in the method: $wpdb->get_results(‘query’, output_type); output_type has the following description: One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information. OBJECT – result will be output as a … Read more

WordPress is trimming leading 0s

%d means integer, integers don’t have leading zeros. Zip codes aren’t numbers in the sense that they quantify something, they’re strings that happen to be made up of numbers, so treat them as strings.

How to display a specific category using a custom Query?

If you insist on continuing on this path using SQL queries, you should first understand how taxonomy terms connect to posts: $wpdb->posts.ID <–> $wpdb->term_relationships.object_id $wpdb->term_relationships.term_taxonomy_id <–> $wpdb->term_taxonomy.term_taxonomy_id $wpdb->term_taxonomy.term_id <–> $wpdb->terms.term_id With this in mind, and assuming you’re providing the $taxonomy (string) and $term_slug (string) parameters for the category you’re filtering by, I envision your code … Read more

Trying to Connect to different database

I’m looking at wp-db.php and see: 1384 if ( $this->dbh->connect_errno ) { 1385 $this->dbh = null; This is in the class function db_connect(), which is called by the wpdb constructor. In the latest error you describe: Warning: mysql_error() expects parameter 1 to be resource, boolean given we can see that mysql_error() is not receiving the … Read more

Counting posts with argument without retrieving the posts

Here’s one thing you could do. Create a WP_Query object but only include ids: $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘books’, ‘s’ => $s, ‘fields’ => ‘ids’, ); $query = new WP_Query( $args ); $post_counts = $query->post_count; wp_reset_postdata(); By using fields => ids you’re only returning the IDs. Also, WP_Query already does a … Read more

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