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

How to pass orderby params to $wpdb->prepare()?

You don’t need to use $wpdb->prepare() for ORDER BY clauses. $wpdb->prepare() will always quote your variables. Supposing you receive the ordering in the request, you can prevent SQL injection by not using the user entered value at all: $sql = “SELECT….”; if ( ‘asc’ == $_GET[‘order’] ) { $sql .= ‘ ORDER BY p.post_date ASC’; … 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”;

How to access custom WordPress database tables

Are you sure that’s the table name? Usually on install WordPress prompts you to setup a table prefix so the default isn’t wp_. Additionally, you need to call the global $wpdb object so it’s accessible to use: global $wpdb; $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” ); echo “<p>User count is {$user_count}</p>”; Finally, to prevent … Read more

$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

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