• Resolved giro2000

    (@giro2000)


    Hi. I have this php code :
    $results = $wpdb->get_results(“SELECT country, COUNT(*) FROM $wpdb_table)
    $results is a associative bi-demensional array, 1rt coloumn named country, the 2nd COUNT(*)
    then I would like to retrieve data from each row but if I am able to read 1rst coloumn values, I cannot with second one :
    if (count($results) > 0) {
    for($x = 0; $x < $arrlength; $x++) {
    echo “country: ” . $results[$x]->country;
    echo “reads: ” . $results[$x]->COUNT(*). “
    “;
    }
    } else {
    echo “0 results”;
    }
    $results[$x]->COUNT(*) does not access any field !!!!
    Might anyone tell me why ? Is * a special character ? how do I have to handle it ? Any other php functions I might use ?
    Thanks a lot to anyone

Viewing 2 replies - 1 through 2 (of 2 total)
  • Topher

    (@topher1kenobe)

    You want to name your count like this:

    $results = $wpdb->get_results(“SELECT country, COUNT(*) as count FROM $wpdb_table)

    Then you can use “count” as you would any other field name. You can name it anything, I just used the word count.

    Also, I’d recommend looking at foreach instead of a for loop. Your way will work fine, but foreach is less code.

    Thread Starter giro2000

    (@giro2000)

    It works. Thanks so much!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘MySQL and PHP arrays handling’ is closed to new replies.