Monday, September 16, 2013

WordPress $wpdb Object Actions

Hiiiii, those who works on wordpress must be aware of the global object $wpdb and those who are new to wordpress, I will be happy to tell you that wordpress is having an object namely $wpdb. By this object you can fetch various values like the prefix of wordpress tables in you database and you also run many functions with the help of this object like database records insertion or retrieval.
wordpress $wpdb

For using this $wpdb object in you wordpress plugins or widgets, you just have to write a line GLOBAL $wpdb; on the top and then you can use this object .So guys in this post I will be explaining only some uses of $wpdb object and  they are as follows :

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );

SELECT a variable :
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );

SELECT a row :
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10");

SELECT a column :
<?php $wpdb->get_col( 'query', column_offset ); ?> 

INSERT a data :
$wpdb->insert( 
 'table', 
 array( 
  'column1' => 'value1', 
  'column2' => 123 
 ), 
 array( 
  '%s', 
  '%d' 
 ) 
);

LAST Insert ID :
$wpdb->insert_id

UPDATE a data : 
$wpdb->update( 
 'table', 
 array( 
  'column1' => 'value1', // string
  'column2' => 'value2' // integer (number) 
 ), 
 array( 'ID' => 1 ), 
 array( 
  '%s', // value1
  '%d' // value2
 ), 
 array( '%d' ) 
);

DELETE rows :
// Default usage.
$wpdb->delete( 'table', array( 'ID' => 1 ) );

// Using where formatting.
$wpdb->delete( 'table', array( 'ID' => 1 ), array( '%d' ) );
 
 
Hope you like this post……..Please Comment…………!!!!!!!!!
Originally posted at Mycodestock.
 


Tags:

0 Responses to “WordPress $wpdb Object Actions”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks