Instead of using $wpdb->query()
, use $wpdb->get_results()
. This method allows you to specify a return type so the query is returned as an associative array. For example …
$results = $wpdb->get_results(
"SELECT * FROM mb_download_manager
WHERE filename="" . $namemeta . ".zip"",
ARRAY_A
);
This will return you an indexed array of associative arrays … so you can iterate through the results:
foreach( $results as $row ) {
if ( $row['downloads'] == '') // ... and so on
If you get stuck, double check the Codex documentation.
Edit 1/13
If you want to add up all of the downloads, you need to do two things:
- Keep a running total
- Properly cast your variable as a number.
So before you do your foreach
loop, declare a download counter:
$counter = 0;
foreach( $results as $row ) {
// etc ...
This variable will hold a running total of your downloads.
Next, you need to add the number of downloads to your counter:
$counter = 0;
foreach( $results as $row ) {
$counter += intval( $row['downloads'] );
// etc ...
The intval()
function will take in a mixed variable and return it’s integer value. So the string "0"
becomes the integer 0
. This allows you to actually add the results rather than concatenating the strings (for example, "1" + "2" = "12"
but 1 + 2 = 3
).