Example code to get a value from the database into a variable:
[php]
$variable = $wpdb->get_var(
$wpdb->prepare(‘
SELECT column3
FROM wp_table
WHERE column1
= %s
AND column2
= %d
‘,
$var1,
123
)
);
[/php]
Example code to get an entire row from the database. Note – the output type may be specified:
- OBJECT, or no specification, results in an object.
- ARRAY_A results in an associative array.
- ARRAY_N results in an indexed array.
[php]
$variable = $wpdb->get_row(
$wpdb->prepare(‘
SELECT column3
FROM wp_table
WHERE column1
= %s
AND column2
= %d
‘,
$var1,
123
),
ARRAY_A
);
[/php]
Example code to get generic results from the database. Note – the output type may be specified (Defaults to OBJECT):
- OBJECT – result will be output as a numerically indexed array of row objects.
- OBJECT_K – result will be output as an associative array of row objects, using first column’s values as keys (duplicates will be discarded).
- ARRAY_A – result will be output as a numerically indexed array of associative arrays, using column names as keys.
- ARRAY_N – result will be output as a numerically indexed array of numerically indexed arrays.
[php]
$variable = $wpdb->get_results(
$wpdb->prepare(‘
SELECT column3
FROM wp_table
WHERE column1
= %s
AND column2
= %d
‘,
$var1,
123
),
OBJECT
);
[/php]
Share/Ask