Professional CodeIgniter

(singke) #1

Chapter 3: A 10,000 - Foot View of CodeIgniter


58


You can even use the from() method to tell CodeIgniter which table to select from, like this:

$this- > db- > select(‘id, name, gender’);
$this- > db- > from(‘persons’);
$Q = $this- > db- > get();

Furthermore, you can list various where provisions using the aptly named where() clause:

$this- > db- > select(‘id, name, gender’);
$this- > db- > from(‘persons’);
$this- > db- > where(‘id’, 14);
$this- > db- > where(‘status’, ‘live’);
$Q = $this- > db- > get();

//query becomes:
//select id, name, gender from persons where id=14 and status=’live’

You could also get fancy with the where() method, passing in operators, such as not equal ( != ) or less
than ( < ):

$this- > db- > select(‘id, name’);
$this- > db- > from(‘persons’);
$this- > db- > where(‘id > =’, 3);
$Q = $this- > db- > get();

//query becomes:
//select id, name from persons where id > = 3

If you ’ re impatient or need to pass in a custom where string, you can do that too:

$this- > db- > select(‘id, name’);
$this- > db- > from(‘persons’);
$this- > db- > where(“name=’Tom’ and id > 3”);
$Q = $this- > db- > get();

//query becomes:
//select id,name from persons where name=’Tom’ and id > 3

The like() method generates SQL LIKE clauses, which is extremely useful when building search
functions:

$this- > db- > select(‘id, name’);
$this- > db- > from(‘persons’);
$this- > db- > like(‘name’, ‘Tom’);
$Q = $this- > db- > get();

//query becomes:
//select id,name from persons where name like ‘%Tom%’

When doing an insert, first you build a data array and pass that array to the insert() method. In many
cases, your data array consists of information from a form, but it could also contain data from a session
or cookie.
Free download pdf