Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Counting Database Entries

Comments in 'Plugin Development' started by Tethered_, Dec 24, 2014.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I have a table that has 2 columns. How can I count how many rows contain the specified value of a column and return this value?

    What I'm doing and theoretically should work in my mind:
    PHP:
    $query $this->db->query("SELECT COUNT(*) FROM master WHERE faction='$faction';");
    $number $query->fetchArray(SQLITE3_ASSOC);
    return 
    $number;
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why don't you test it and var_dump the fetched array yourself?

    Hint: use AS to make the key easier.
    EvolSoft and iksaku like this.
  3. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I've tested and dumped and tested and dumped and all over again. I can't seem to figure it out... Can I get help?
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What is the dumped result?
  5. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You mean $number is always 0 (int)?
  7. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    PHP:
    array (
      
    'COUNT(*)' => 0,
    )
    I get this no matter what
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What if you select *, it does return the rows?
  9. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    What do you mean by select *?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Normal table selection.
    SELECT * FROM master WHERE faction='$faction';
  11. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Just tried that. I then call the
    PHP:
    count()
    method on that variable that held the query. I'm now always getting 1...
    Last edited: Dec 24, 2014
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In SQLite3 extension, every time you fetchArray() it returns a new result row. You should while(is_array($row = fetchArray())).
  13. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Remember that the first thing to do when you make a plugin (or other things) with arrays, strings, database values, ... is to print the output ;)
    Last edited: Dec 24, 2014
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    num_rows method:
    $result = $this->db->query("SELECT COUNT(*) FROM master WHERE faction='$faction'");
    $count = $result->num_rows;
    $value = $count[0];

    or with fetch array method:
    $result = $this->db->query("SELECT COUNT(*) as count FROM master WHERE faction='$faction'");
    $count = $result->fetchArray();
    $value = $count['count'];
    Last edited: Dec 24, 2014

Share This Page

Advertisement