wordpress database queries format
- by admin
- 0
Writing Custom Queries in WordPress
Format
===============================
$result = $wpdb->get_results(query); // any query inside it.
or
$wpdb->query(query here)
// Select table
global $wpdb;
$tables = $wpdb->get_results(“show tables like ‘{$wpdb->prefix}woocommerce_bonus_point'”);
// Create table
global $wpdb;
if ( ! empty($wpdb->charset) )
$charset_collate = “DEFAULT CHARACTER SET $wpdb->charset”;
if ( ! empty($wpdb->collate) )
$charset_collate .= ” COLLATE $wpdb->collate”;
$wpdb->query(“CREATE TABLE {$wpdb->prefix}woocommerce_bonus_point (
bonus_point_id bigint(20) unsigned NOT NULL auto_increment,
bonus_point_type varchar(255) default NULL,
bonus_point_type_name varchar(255) default NULL,
bonus_point_type_value bigint(20),
bonus_point_type_real_name varchar(255) default NULL,
PRIMARY KEY (bonus_point_id)
) $charset_collate;”);
// Select table fields
global $wpdb;
$table = $wpdb->prefix.”usermeta”; // Here “usermeta” table
$users = $wpdb->get_results(“SELECT DISTINCT(user_id) FROM $table WHERE meta_key =’last_name'”);
Writing Custom Queries in WordPress Format =============================== $result = $wpdb->get_results(query); // any query inside it. or $wpdb->query(query here) // Select table global $wpdb; $tables = $wpdb->get_results(“show tables like ‘{$wpdb->prefix}woocommerce_bonus_point’”); // Create table global $wpdb; if ( ! empty($wpdb->charset) ) $charset_collate = “DEFAULT CHARACTER SET $wpdb->charset”; if ( ! empty($wpdb->collate) ) $charset_collate .= ” COLLATE $wpdb->collate”;…
Writing Custom Queries in WordPress Format =============================== $result = $wpdb->get_results(query); // any query inside it. or $wpdb->query(query here) // Select table global $wpdb; $tables = $wpdb->get_results(“show tables like ‘{$wpdb->prefix}woocommerce_bonus_point’”); // Create table global $wpdb; if ( ! empty($wpdb->charset) ) $charset_collate = “DEFAULT CHARACTER SET $wpdb->charset”; if ( ! empty($wpdb->collate) ) $charset_collate .= ” COLLATE $wpdb->collate”;…