A small cheat sheet of wp’s database functions.
In your own functions put this line of code to be able to use wp’s db:
global $wpdb;
Get a field
$field = $wpdb->get_var($sql);
Get a single row
$row = $wpdb->get_row($sql);
Get some rows
$rows = $wpdb->get_results($sql);
Escape an input
$value = $wpdb->get_escape($input);
Perform a query
$n = $wpdb->query($sql);
WP Tables’ name
You can use
- $wpdb->links
- $wpdb->posts
- …
- and even $wpdb->prefix !
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_N);
Return format
In get_row and get_results you can specify in the second argument the format
- OBJECT – result will be output as an object.
- ARRAY_A – result will be output as an associative array.
- ARRAY_N – result will be output as a numerically indexed array.
More info
Function Reference/wpdb Class « WordPress Codex.
$mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_N);