QuestionsDatabase: The wpdb queries
mmWebetutorial Staff asked 6 months ago

Retrieve and display the number of users:
$user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” );
echo “<p>User count: {$user_count}</p>”;

Get all the information about row Link 11: $link = $wpdb->get_row( “SELECT * FROM $wpdb->links WHERE link_id = 11” );

SELECT Generic Results:
$fivespub = $wpdb->get_results( “SELECT ID, post_title FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_author = 2”);

foreach ( $fivespub as $fivespu )
{
echo $fivespu->post_title;
}

Insert two columns in a row:
$wpdb->insert( ‘tablename’,
array(
‘column1’ => ‘value1’,
‘column2’ => ‘value2’
),
array(
‘%s’,
‘%d’
)
);

Update a row in the table:$wpdb->update(
‘table’,
array(
‘column1’ => ‘value1’, // string
‘column2’ => ‘value2’ // integer (number)
),
array( ‘ID’ => 1 ),
array(
‘%s’, // value1
‘%d’ // value2
),
array( ‘%d’ )
);

DELETE Rows:
$wpdb->delete( ‘table’, array( ‘ID’ => 1 ) );

Your Answer
Login
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.