How to convert an array variable to a single variable.

Discussion in 'MySQL' started by tommytx, Sep 11, 2016.

  1. #1
    I have the output of the code below in a count command and need to get the answer into a single variable... like $dog = 88; but so far unsuccessful.. is there a simple way to do it..
    I have tried $result[0] and $result[total] when I temp changed the query to as total but nothing seems to work...

    (
      [0] => stdClass Object
      (
      [COUNT(*)] => 88
      )
    
    $results = $wpdb->get_results('SELECT COUNT(*) FROM wp_realty_listingsdb WHERE DOM = 0');
    print "<pre>";
    print "Total DOM 0 = ";
    print_r($results);
    print "</pre>";
    Code (markup):

     
    Last edited by a moderator: Sep 12, 2016
    tommytx, Sep 11, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    This should work for the as total version: $result[0][total]
     
    PoPSiCLe, Sep 11, 2016 IP
  3. tommytx

    tommytx Well-Known Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Thank you for you assistance however that did not work either but i found a solution.
     
    tommytx, Sep 11, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Then you should post the solution here, so that others can find it if they're looking for a solution to that problem.
     
    PoPSiCLe, Sep 12, 2016 IP
    sarahk likes this.
  5. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #5
    Well if anyone is curios since he's using wpdb it has some parameters you can pass to it
    One of three pre-defined constants. Defaults to OBJECT.
    • 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.
    
    
    
    
    $results = $wpdb->get_results('SELECT COUNT(*) FROM wp_realty_listingsdb WHERE DOM = 0',ARRAY_N);
    
    Code (php):
    Then just $result[0][0] . Hope that helps anyone else with that issue.
     
    Einheijar, Oct 7, 2016 IP