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

Connect to DB

Discussion in 'XenForo Development Discussions' started by ruaduck, Dec 16, 2015.

  1. ruaduck

    ruaduck Member

    I know i can connect to my Database using the following:
    Code:
    $db = XenForo_Application::get('db');
    $query = $db->fetchRow("SELECT * FROM Event_Feeder ORDER BY id DESC");
    $date = $query['date'];
    $text = $query['text'];
    $color = $query['color']
    
    However that only grabs one row. How can i do it so that it will fetch multiple rows?
     
  2. wang

    wang Well-Known Member

    Use the fetchAllKeyed() function.
     
  3. ruaduck

    ruaduck Member

    using that function how would i go about getting the information that is presented? Is it in an array? Just want to make sure i am doing the syntax correctly .
     
  4. Jake B.

    Jake B. Well-Known Member

    It's an array of all of the entries with the key that you set and each value in the parent array is an array of the fields in the table
     
  5. ruaduck

    ruaduck Member

    So would this be a correct assumption of the array if i were to pull the date?
    $date = $query['date'[0]];
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    This function isn't available from the DB object - it's only available in model classes.

    So it would be:
    Code:
    $query = $db->fetchAll("SELECT * FROM Event_Feeder ORDER BY id DESC");
    That does output the results of the query as an array. The keys are numeric starting at 0.
     
    Robust, wang, ruaduck and 1 other person like this.
  7. Chris D

    Chris D XenForo Developer Staff Member

    Well, it would be:
    Code:
    $date = $query[0]['date'];
    Where 0 is the first row returned, 1 is the second row, 2 is the third row, etc.
     
    ruaduck likes this.
  8. ruaduck

    ruaduck Member

    Thank you Chris. You are a life saver.
     
  9. Jake B.

    Jake B. Well-Known Member

    expanding on that it may be a good idea (depending if that table will ever be empty) have an if (isset($query[0])) wrapping that so you'd have this:

    PHP:
    if (isset($query[0])) {
        
    $date $query[0]['date'];

        
    // everything else in here as well
    }
    Otherwise you'll get an error that index 0 doesn't exist if that table is empty
     
    ruaduck likes this.
  10. Chris D

    Chris D XenForo Developer Staff Member

    Well, indeed. That goes without saying (well, actually, it doesn't, good point :p). It depends on exactly how the data is being used, though. I would guess it would be unlikely to access individual keys, like that, unless you're specifically after the first record, in which case, you might want to use reset() as that might be a bit clearer (though you'd still want to check the date key isset). The aim may well be just to see if any data is returned at all, so you can just get away with:
    PHP:
    if (!$query)
    {
        
    // no data
    }
     
    ruaduck and Jake B. like this.
  11. Robust

    Robust Well-Known Member

    Usually keys aren't accessed directly like that, it's a case by case scenario type thing though. If processing through multiple, indefinite rows then you'd use a foreach loop. So first you'd make sure the query has data and then just foreach through it, or a simple foreach would do anyway (as if there's no data, it won't really loop... anything...)
     

Share This Page