How do I count columns on a custom WPDB query?

Under straight mysql core functions you would use mysql_num_fields, but through $wpdb (wordpress database object), I found the following worked for me. $result = $wpdb->get_results(‘DESCRIBE ‘.$table,ARRAY_A); $columns = array(); foreach($result as $row) { $columns[] = $row[‘Field’]; } $num_fields = count($columns);

Getting variable from Database

Prepend the table prefix to iyzico_order_refunds instead of the $wpdb object: $payment_transaction_id = $wpdb->get_var( “SELECT payment_transaction_id FROM {$wpdb->prefix}iyzico_order_refunds} WHERE order_id=769 AND item_id=760” );

get_results not returning anything

$wpdb->get_results() returns an array on success, so you can’t simply echo $result;. Instead, you can use foreach to loop through the results and display whatever the data that you want to display: $results = $wpdb->get_results( $q ); foreach ( $results as $row ) { echo $row->meta_value . ‘<br />’; } But I can see that … Read more

Why does wpdb->update delete other meta?

Your UPDATE command would look like so after all variables are substituted — and in this example, the table prefix is wp_ (the default one), $group[‘1C_id’] is 9095b4cf-969d-11e9-a601-5cf3706390c8, and $found_id (the term ID) is 123. UPDATE `wp_termmeta` SET `meta_key` = ‘1C_id’, `meta_value` = ‘9095b4cf-969d-11e9-a601-5cf3706390c8’ WHERE `term_id` = 123 So that means, all existing meta where … Read more

wordpress $wpdb works only once

Don’t bother using wpdb to update post content, use wp_update_post instead! http://codex.wordpress.org/Function_Reference/wp_update_post Instead of $wpdb->update($seo_tblPosts, array(‘post_content’=>$seo_pageContents), array(‘ID’=>$row_id)); Do: // Update post 37 $my_post = array(); $my_post[‘ID’] = $row_id; $my_post[‘post_content’] = $seo_pageContents; // Update the post into the database wp_update_post( $my_post ); As a side benefit it fires all the hooks so caching works as it … Read more

Why does this WPDB code throw an empty WPDB error? [closed]

The code you’ve linked to above: //$sql set to raw SQL statement $sql = $wpdb->prepare($sql,$feed_id); if($wpdb->query($sql)){ $resp->uf_error = $wpdb->print_error(); } So if there isn’t, an error you try displaying the ‘error’ with $wpdb->print_error(). Also$wpdb->print_error() doesn’t return an error, it prints it. It’s (probably) the printing of that non-error that is causing an error in your … Read more

WordPress db prepare

From the Codex, 2nd parameter for the prepare() method: (int|string|array) The value to substitute into the placeholder. Many values may be passed by simply passing more arguments in a sprintf()-like fashion. Alternatively the second argument can be an array containing the values as in PHP’s vsprintf() function. Care must be taken not to allow direct … Read more

PHP Fatal Error – $wpdb a non-object?

“the errors (…) don’t show a stack trace” – they would if you were using xdebug. It means $wpdb is not loaded yet, not loaded properly, or gone: If you added code that calls it to your wp-config file or something to that order, remove it. If you’ve a db.php file in wp-content, it’s probably … Read more

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