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

Reading from the database.

Discussion in 'XenForo Development Discussions' started by Renari, Sep 13, 2012.

  1. Renari

    Renari Member

    I can't seem to find documentation anywhere on reading from the database.

                $db XenForo_Application::get('db');
    $result $db->query("SELECT `user_id` FROM `xf_admin`");
                while (
    $result) {
    $admin .= $result['user_id'];
  2. Chris D

    Chris D XenForo Developer Staff Member

    Typically you would achieve this using the XenForo model.

    You would create a Code Event Listener that listens to the load_class_model event.

    Then you would create a new Model class that extends the main XenForo_Model class...

    class YourAddOnID_Model_YourAddOn extends XenForo_Model
    public function getAdminUserId($userId)​
    return $this->_getDb()->fetchRow('​
    SELECT *​
    FROM xf_admin​
    WHERE user_id = ?​
    ', $userId);​
    Now all you need to do is call that from your Listener or Controller:

    $admin = XenForo_Model::create('YourAddOnID_Model_YourAddOn')->getAdminUserId($userId);
    There is a way to do it like you said, but the code I've entered above is more typical of how it's done in XenForo which tends to adhere to MVC principles.
    Sadik B and Jake Bunce like this.
  3. Renari

    Renari Member

    I found my answer here: http://framework.zend.com/manual/1.12/en/zend.db.statement.html

                $db XenForo_Application::get('db');
    $query $db->query("SELECT `user_id` FROM `xf_admin`");
                while (
    $result $query->fetch()) {
    $admin .= $result['user_id'];
  4. Renari

    Renari Member

    Is there any specific reason it's done that way?
  5. Chris D

    Chris D XenForo Developer Staff Member

  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    A model is basically an interface for a certain type of data. There is a user model, a thread model, etc. Models are good if many different areas will need to query the same information. You can create a function in the model and have everyone call that function.

    Model or not, I suggest using one of the fetch functions when running your query. Instead of this:

    $db->query("SELECT `user_id` FROM `xf_admin`");
    Use this:

    $db->fetchCol("SELECT `user_id` FROM `xf_admin`");
    There are different fetch functions:

    fetchAll = returns an array of rows
    fetchRow = returns one row (which is an array)
    fetchCol = returns an array of individual column values for multiple rows
    fetchOne = fetches just one column for one row (not an array)

    Which fetch function you use depends on the results of your query. But the fetch functions are nice because they allow you to deal in arrays and primitive types instead of mysql resources.
    Chris D likes this.
  7. Renari

    Renari Member

    Thanks guys, I don't think the modal will be necessary since this query will only ever be run in this location.

Share This Page