WordPress is very popular open source in PHP. WordPress has number inbuilt functionality which is baseic for any website and blog.Here we will discuss how to insert and update record using wordpress connection object. WordPress defines a class called
wpdb, which contains a set of functions used to interact with a database. Its primary purpose is to provide an interface with the WordPress database, but can be used to communicate with any other appropriate database.
There are two methods to access $wpdb object:
1- $wpdb : Declared
$wpdb as global and used it to execute an SQL query statement that returns a PHP object.
$results = $wpdb->function
2- $GLOBALS : Access
$GLOBALS superglobal. Does not require global keyword (but may not be best practice).
$results = $GLOBALS['wpdb']->function
How to insert record using $wpdb:
We can use insert method to insert data into database.
$wpdb->insert('table_name', array('id'=>'1', 'name'=>'parvez'));
How to update record using $wpdb:
We can use update method to insert data into database.
$wpdb->update('table_name', array('id'=>'1', 'name'=>'parvez', array('Id' => '2')));