QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Creating WordPress Tables and Storing Data

Creating WordPress Tables and Storing Data

wordpress-tables

This post is a part 12 of Creating a WordPress Admin Theme post series.

In this article I will show you how you can create wordpress tables and also how to store data in wordpress tables. WordPress can be used to store arbitrary data(data not related to wordpress settings, posts/pages, profile, wordpress functionality etc).


What is WordPress Tables

WordPress tables are MySQL tables created using WordPress API and their name starts with WordPress defined prefix.

WordPress $wpdb global object

$wpdb global object is used to retrieve the prefix of wordpress tables, add rows to wordpress tables and get rows from wordpress tables.

Retrieving Prefix

echo $wpdb->prefix;

For multisite wordpress installation prefix property returns different prefixes for each site in the network.

Adding Row

$wpdb->insert('table', array('name' => 'narayan', 'id' => 34), array('%s', '%d'));

The insert method has 3 parameters, the table name, an array representing the row and the columns data format.

Retrieving Rows

$results = $wpdb->get_results($wpdb->prepare('SELECT * FROM  tablename WHERE name="narayan"'));

Iterating Result

foreach ($results as $row) {
        echo $row->name;
        echo $row->ID;
}

dbDelta function

dbDelta function is used to create WordPress tables.

$table_name = $wpdb->prefix . "tablename";
$sql = "CREATE TABLE $table_name (
      id INT NOT NULL ,
      name varchar(255) NOT NULL
    );"
;
/*To use this function you must include it.*/
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

If the table exists then dbDelta will not create a table, it will just ignore.

Sep 27, 2014Narayan Prusty
Change Navigation Style On ScrollStore Your WordPress Users Raw Password

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

Image8 years ago WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • What is WordPress Tables
  • WordPress $wpdb global object
  • dbDelta function
Related Articles
  • WordPress $notoptions Array
  • WordPress Multisite Database Table Structures
  • wp_load_alloptions() Function
  • Local Database Storage using Intel XDK
  • Customizing WordPress Admin Profile Page
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license