How to fetch Data in WordPress using MySQLi or $wpdb

To fetch data from database table <?php $results = $wpdb->get_results( “SELECT * FROM $table_name”); // Query to fetch data from database table and storing in $results if(!empty($results)) // Checking if $results have some values or not { echo “<table width=”100%” border=”0″>”; // Adding <table> and <tbody> tag outside foreach loop so that it wont create … Read more

wpdb update add current timestamp not working

You seemed to have solved everything but the issue with the time: found the issue %d should be %s. but I see that it saves the server time not the current timezone WordPress has a number of date/time related functions. In this case, it sounds like what you need is current_time(), which… Returns the blog’s … Read more

Does dbDelta delete columns as well?

As far as I know, dbDelta() is primarily used to add tables to the database. It can also add or alter columns. To delete columns, you should use $wpdb->query(): global $wpdb; $table = $wpdb->prefix . ‘table_name’; $wpdb->query( “ALTER TABLE $table DROP COLUMN column_name” );

Detecting errors generated by $wpdb->get_results()

There is a class variable that stores the last error string – $wpdb->last_error. By the looks of the way $wpdb is coded, if the query succeeds, $wpdb->last_error will be an empty string, if it fails, it will be the error string returned by MySQL. So something like this would do the trick. $result = $wpdb->get_results(“SELECT … Read more

Get error messages when $wpdb->insert() returns false?

$wpdb->insert() method returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1). You can turn error echoing on and off with the show_errors and hide_errors, respectively. <?php $wpdb->show_errors(); ?> <?php $wpdb->hide_errors(); ?> You can also print the error (if any) generated by the … Read more

How do I check for a duplicate record before inserting using wpdb

Let’s say the primary key of the table is my_part_ID. So we will check if there is any primary key value for the combination of user_ID and PL_part_ID as below $my_part_ID = $wpdb->get_var( $wpdb->prepare( “SELECT my_part_ID FROM ” . $wpdb->prefix . “pl_my_parts WHERE user_ID = %d AND PL_part_ID = %d LIMIT 1”, $user_ID, $PL_part_ID ) … Read more

Fetch array with $wpdb

wpdb‘s get_results method takes an optional second argument that lets you specify how the data is returned. The default return is an object. But you can also set it to… OBJECT – result will be output as a numerically indexed array of row objects. OBJECT_K – result will be output as an associative array of … Read more

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