Connect to DB

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?
 
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 .
 
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 .

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
 
So would this be a correct assumption of the array if i were to pull the date?
$date = $query['date'[0]];
 
Use the fetchAllKeyed() function.
This function isn't available from the DB object - it's only available in model classes.

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?
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.
 
So would this be a correct assumption of the array if i were to pull the date?
$date = $query['date'[0]];
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.
 
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.

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
 
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
}
 
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
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...)
 
Top Bottom