1. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.
  2. In order to post messages and view attached files in this forum, you must be a licensed XenForo customer.
    If you can't post, enter your forum username in the Associated Forum Users form in your customer area.

MySQL Queries - How to use $db in XenForo & small notes

Discussion in 'Development Tutorials [Archive]' started by Facepalmx2, Oct 7, 2010.

  1. Facepalmx2 Active Member

    dmnkhhn is correct. I do believe, extending the XenForo_Model class, you can simply type fetchAllKeyed(...) instead of doing $this->_getDb() .. etc.
  2. ragtek back2life

    fetchAllKeyed is a method of the Model Class AND NOT from ZEND DB

    so you have to call it direct ;)
    For example:
    XenForo_Model_AddOn
    PHP:
        public function getAllAddOns()
        {
            return 
    $this->fetchAllKeyed('
                SELECT *
                FROM xf_addon
                ORDER BY title
            '
    'addon_id');
        }
    dmnkhhn likes this.
  3. Facepalmx2 Active Member

    Correct, this has been established. Can you just do fetchAllKeyed() or does the code require you do $this->fetchAllKeyed()?
  4. ragtek back2life

    Methods can only be run with $this-> like in this case (or self:: or parent:: or classname:: for static methods) ;)
  5. Facepalmx2 Active Member

    Odd, didn't know that. As far as PHP goes anyways. Learn something new everyday, eh?

    I will play around with the models and see what documentation comes up. Thanks
  6. ragtek back2life

    Yea, developers life is always full off new things:)
  7. Luciano Member

    This stuff is new to me also, just one stupid question, so please dont all laugh at the same time...
    Using this, but at any moment to I need (in some cases) to do what we used to in vb:
    either unsetting some arrays and or $db->freeresult()....
    ?
  8. Kier XenForo Developer

    No, you don't need to worry about cleanup like that in the vast majority of cases. It's micro-optimisation anyway, and largely pointless.
    Dave Stephenson and jadmperry like this.
  9. bambua Well-Known Member

    Theoretically doesn't the server just take the class out of memory(actually the whole php script for that matter) as soon as it's done completely executing the page?
  10. dmnkhhn Active Member

    One thing I find noteworthy about the queries. :)

    Instead of doing
    PHP:

    $this
    ->_getDb()->fetchAll('
            SELECT *
            FROM xyz
            WHERE abc = 1'
    );
    you could also use:
    PHP:

    $this
    ->_getDb()->fetchAll('
            SELECT *
            FROM xyz
            WHERE abc = ?'
    1);
    The '?' acts as a placeholder, the 1 is the value that is used instead.
    If I read the code correctly, you can even use multiple '?' as long as you provide an array of data to bind into those placeholders.

    It's called 'prepared statements', you can read more about it at: http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
    DarkSign likes this.
  11. Robbo Well-Known Member

    It seems a bit weird to me that you can't query the number of rows faster. I don't see how it could be good for performance to have to query every row of a table and then use count() on it. Come to think of it COUNT(*) would be better then what you have said.

    So instead of
    $numRows = count($db->fetchAll("SELECT * FROM `xf_user`"));

    Do...
    list($numRows) = $db->fetchRow("SELECT COUNT(*) FROM `xf_user`");

    Note: I am fairly tired so may have messed something up there.
  12. dmnkhhn Active Member

  13. Kier XenForo Developer

    PHP:
    $numRows count($db->fetchAll("SELECT * FROM `xf_user`"));
    Should never appear, it's a huge waste of resources. The only time PHP's count() should be used in this way is to count the number of records from a data set that has been fetched already for its own purpose:
    PHP:
    $things $db->fetchAll('SELECT * FROM xf_thing');

    $numThings count($things);
    If you just need to count the number of things in the database, use SQL's COUNT(*).
    PHP:
    $numThings $db->fetchOne('SELECT COUNT(*) FROM xf_thing');
    DarkSign likes this.
  14. Robbo Well-Known Member

    Yeah I was really hoping they weren't right in the OP. And I was close to getting it right, guess I should actually look at the database layer oneday :p
  15. physicspirate Active Member

    How do i create a new table where the primary key is the logged in user ID, and it stores a URL, and a text field for each user that exists in the main database? I want to add some profile fields to my user profile pages, but not sure how to do this.
  16. Lu Jia Active Member

    To return a single column from the next row of the result set, use fetchColumn(). The optional argument is the integer index of the column, and it defaults to 0. This method returns a scalar value, orFALSE if all rows of the result set have been fetched.

    Note this method operates differently than the fetchCol() method of the Adapter class. ThefetchColumn() method of a statement returns a single value from one row. The fetchCol() method of an adapter returns an array of values, taken from the first column of all rows of the result set. (source)

    To retrieve a row from the result set structured as an object, use the fetchObject(). This method takes two optional arguments. The first argument is a string that names the class name of the object to return; the default is 'stdClass'. The second argument is an array of values that will be passed to the constructor of that class.

    There are 3 more fetch method if someone need :)
  17. Liam O'Neill New Member

    What pages do I need to include to access the DB externally - in a wordpress page for example.

    Thanks

Share This Page