Part of the EllisLab Network
   
 
[Documentation] Database
Posted: 04 December 2006 06:33 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  254
Joined  10-04-2006

Hi all,

I think there is a problem with database documentation.
It is not an error because it works but I think it is not clever.

Here’s the code

$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach (
$query->result() as $row)
{
    
echo $row->title;
    echo
$row->name;
    echo
$row->email;
}

echo 'Total Results: ' . $query->num_rows();

The method ‘result()’ is called every time in the foreach. Even if it returns directly the array, it is not the best way to do it.
The foreach is not the best method anymore.
Why do not use a for instead because the key is not use.

I propose this.

$query = $this->db->query('SELECT name, title, email FROM my_table');
$aResult = $query->result();
$iNbRows = $query->num_rows();
for (
$i=0; $i<$iNbRows; $i++ ) {

    $row
= & $aResult[$i];
    echo
$row->title;
    echo
$row->name;
    echo
$row->email;
}
echo 'Total Results: ' . $iNbRows;


I do not agree with object by default too, but it is another problem.

Tell me if I am Wrong;

Thx.

 Signature 

Sylvain Gourvil
PHP Developer
French scuba divingMotivation and team building

Profile
 
 
Posted: 04 December 2006 10:53 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

There’s no significant difference between your code and the current code, except that yours is more lines and less function calls.

 Signature 

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

Profile
 
 
Posted: 04 December 2006 11:34 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  254
Joined  10-04-2006
Shadowhand - 04 December 2006 10:53 AM

more lines and less function calls.

I agree with that.

But for a website with millions unique visits and huge ressources, it has a difference.
I know it is not a huge stuff but I told it only because it is a doc.

 Signature 

Sylvain Gourvil
PHP Developer
French scuba divingMotivation and team building

Profile
 
 
Posted: 04 December 2006 11:58 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
RankRankRank
Total Posts:  404
Joined  02-14-2007

Completely different topic, but still on database documentation:  A while ago, Rick, you changed the DB so we could pass an existing connection into CI by setting the db[‘conn_id’] variable in our config file.  However, the DB documentation fails to reflect this change.  Works perfectly, though.  smile

Profile
 
 
Posted: 04 December 2006 01:13 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
RankRankRank
Total Posts:  970
Joined  04-13-2006
Tchinkatchuk - 04 December 2006 06:33 AM

....I do not agree with object by default ....

Why not, Sylvain?

Profile
 
 
Posted: 04 December 2006 01:32 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  11-29-2006

Tchinkatchuk is correct in that his proposed code would be faster. Here’s why:

- The use of foreach operates on a copy of the array, not the original. Hence more overhead.
- By storing num_rows() in a variable, the duration of the for loop is known ahead of time and it avoids repeated function calls to check and see if there are more records to be read. Again, you’re doing less work.

Now, the first issue could be alterated to work on the original copy of the results by doing:

foreach (&query->result() AS $row)

And…

$row = & $aResult[$i];
// should be replaced with:
$row = $aResult[$i];

In the end, there is a difference between the two versions. However for most developers, foreach is a little more friendly. I too prefer results being returned in an array, but it’s just as easy to use result_array() as result().

Profile
 
 
Posted: 05 December 2006 03:35 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  09-22-2006

I agree with Negligence and Sylvain.
Foreach is a bit longer, for reasons explain by Negligence, like copying array for parsing him. And you can’t modify this array in a foreach, but it’s not the question smile.
Why a row is smaller than an object ?
Try it yourself in a big system. Default, array are much faster than an object, but less structured.
For small recordset, there is no difference, perhaps some microseconds. But for big recordset, you could see memory difference, especially when you return 1 000 000 rows and don’t understand why apache server don’t like it smile
I have just change this param (object) and my serv go on.

 Signature 

Web Project Manager for e-commerce

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: 66432 Total Logged-in Users: 32
Total Topics: 84799 Total Anonymous Users: 2
Total Replies: 455096 Total Guests: 231
Total Posts: 539895    
Members ( View Memberlist )
Newest Members:  GlennJHurlyBurlyDylan1978X_franbaguasllogocsaturkeyPeter BryanttherendStudioGeorgiaJ