Continuing on from day 1, today, I’ll teach you five different ways to write select statements for your database. If you haven’t watched the first entry in this video series, don’t worry; each video can function on its own as a single tutorial. Having said that, I highly recommend that you watch each screencast.
Catch Up
Day 2
- Follow us on Twitter, or subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.



RoyalSlider – Touch-Enable ... only $12.00 
Hey Jeff,
I think I noticed an error in your third getAll() method:
Instead of:
function getAll() {
$this->db->select(‘title’, ‘contents’);
$q = $this->db->get(‘data’);
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
It should be:
function getAll() {
$q = $this->db->get(‘data’);
$this->db->select(‘title’, ‘contents’);
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
I assume the logic is that you need to get the db table first before you can get the columns.
No… that’s true, u can check at http://codeigniter.com/user_guide/database/active_record.html
it’s not an error.
the Select method should logically precede the Get method as the former is not doing the actual query but defines it, which should obviously occur before it is executed by the latter.
i know i’m kinda late here, but i was wondering about the error you get at about 18:51 “Invalid argument supplied for foreach()”. I understand it’s because the query doesn’t return anything, but then again this basically renders the if statement useless. How can $q->num_rows() > 0 be true there? Can you please clarify this?
I believe it’s because of how he has the if statement setup. He is returning $data whether or not the foreach code is run. So if the num_rows() returns 0 then he returns an empty or NULL array and therefore receives the error. In order to avoid this, you would need to have an else that sets the data array to some sort of error message such as “Your query returned no results. Please try again.”
I hope that makes sense.
Hi,
No disrespect meant, but this is really sloppy, and I can’t help feeling that i’m learning bad practices by learning from these tutorials. I had higher expectations from net tuts, to be honest. You’re just making so many mistakes and having to correct yourself, that i’m finding it hard to concentrate.
I know it would take longer to produce the video, but if you re-did bits where you made mistakes, i think it would be much more impressive.
Thanks for making the effort anyway.
Spawn
Pardon my ignorance on the matter but what’s the point of using active records? Isn’t it far more succinct to write one line of SQL instead of 3 lines of AR code:
$this->db->query(” SELECT title, contents FROM data WHERE id = 2 “);
Instead of:
$this->db->select(‘title, contents’);
$this->db->from(‘data’);
$this->db->where(‘id’, 2);
It just seems more verbose to me but then again I know nothing about AR so I could be missing something. Would really like to know what the benefits are.
Actually you can write nested AR-statement. I think the author did it this way to not confuse beginners. Also, using AR makes your app more db agnostic.
Pardon my ignorance on the matter but what’s the point of using active records? Isn’t it far more succinct to write one line of SQL instead of 3 lines of AR code:
$this->db->query(” SELECT title, contents FROM data WHERE id = 2 “);
Instead of:
$this->db->select(‘title, contents’);
$this->db->from(‘data’);
$this->db->where(‘id’, 2);
It just seems more verbose to me but then again I know nothing about AR so I could be missing something. Would really like to know what the benefits are.
Thank You!!! :D
At the very beginning of the day 2 video (0:40 mark), Jeff references changing the name of the exact set of files to ci_day2. I know it must be elementary, but I’m not sure exactly what we’re supposed to do here. Can someone clarify?
Another great tutorial. Thanks for sharing : )
in my opinion active records seems kind of a bad idea. because if your queries become very complex its going to be confusing typing all that out. why not just stick to the method we are all used to writing it all out and sticking it to a variable that way life will be easier in the long run.
hi guys…..
Wonderful tutorials. I just watched first two of them and i cleared a lot of my initial questions
Thanks for the tutorial, I just started working on this framework and this is really helpful…
Great tutorial!!!!!!! Thanks
function getAll() {
$q = $this->db->get(‘data’, 2);
I tried setting the offset like so:
function getAll() {
$q = $this->db->get(‘data’, 2, 5);
and I get error like so:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 10
Does anyone know what/why this is happening?
Vacation. Going over all tutorial submissions today. :)
yeah its named as CI:Dat 2
What? VACATION? Good gawd. *sigh*
*mumbles*….vacation….
:P
Now how to deal with this: The URI you submitted has disallowed characters.
I did a lot of search and people were talking about downgrading to php 5.2 version. May be you have a better answer?