Part of the EllisLab Network
   
 
Extend database query result functions
Posted: 11 February 2007 12:26 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

1. I’d like to be able to fetch numeric arrays from the query result. At the moment only associative arrays are returned.

Could a function like this be added to DB_result.php?

function row_array_numeric($n = 0) {

    
// logic spread over three steps (can be simplified to just 1 line)
    
$associative_array = $this->row_array($n);
    
$numeric_array = array_values($associative_array);
    return
$numeric_array;
}


2. A function that would return only one the first value from a query result would be quite handy as well.

function result_value() {

    $numeric_array
= $this->row_array_numeric();
    
$value = $numeric_array[0];
    return
$value;
}

You could also add an X coordinate parameter to this function that returns $numeric_array[X] if that index exists:

function result_value($x = 0) {

    $numeric_array
= $this->row_array_numeric();

    if (
array_key_exists($x, $numeric_array)) {
        $value
= $numeric_array[$x];
    
}
    
else {
        $value
= $numeric_array[0];
    
}

    
return $value;
}

You could even go further and add an Y coordinate as well.

Anyway, these would be quite useful function in my opinion. What do you think?

 Signature 

Kohana rocks!

Profile
 
 
Posted: 12 February 2007 07:33 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Rank
Total Posts:  67
Joined  09-28-2006

i could be wrong about this, but it seems the functionality for #2 is found in either the row or the row_array functions

http://codeigniter.com/user_guide/database/results.html

Profile
 
 
Posted: 13 February 2007 05:02 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  35
Joined  08-04-2006

I agree with Geert De Deckere on point #1, fetching row (mysql_fetch_row for example) take less resources than fetching array. I think a dedicated method including the database fetch row option will be better. In my opinion with object oriented code (models in mvc) use of associative arrays for database results is not necessary (just never use SELECT *).

Profile
 
 
Posted: 13 February 2007 12:08 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

Let me show off my second point with an example. The function below is the count_all() function straight from /database/drivers/mysql/mysql_driver.php

function count_all($table = '')
{
    
if ($table == '')
        return
'0';

    
$query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
    
    if (
$query->num_rows() == 0)
        return
'0';

    
$row = $query->row();
    return
$row->numrows;
}

Could be simplified to this:

function count_all($table = '')
{
    
// [...]
    
    
return ($query->num_rows() == 0) ? '0' : $query->value();
}

 Signature 

Kohana rocks!

Profile
 
 
Posted: 13 February 2007 02:03 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Your row_array_numeric function makes absolutely no sense at all. All you need to do to get this effect is:

$result = array_values ($query->row_array());

Why would you add a function just for this?

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
Posted: 13 February 2007 02:32 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  248
Joined  02-10-2007

Keeps my code cleaner. Well, yeah… I guess I could live without such a seperate function.

What do you think about the second function? It is not a must-have neither, but again, your code can be simplified. That’s especially the case when using queries where you expect just one value. For example:

SELECT COUNT(1) FROM table;
SELECT FOUND_ROWS();

See also PHP’s mysql_result() function.

 Signature 

Kohana rocks!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 66409 Total Logged-in Users: 26
Total Topics: 84746 Total Anonymous Users: 0
Total Replies: 454805 Total Guests: 222
Total Posts: 539551    
Members ( View Memberlist )
Newest Members:  captainredmuffquinodligtharttechsivamDjordjesammozzazodman23mbsaarchaicharibok