Database Interaction with Codeigniter 1.7

by Adam Griffiths | April 2010 | Open Source PHP Web Development

Having seen form validation in the previous article, we will now cover database interaction. Databases are the backbone behind any Web application. Without a database, you'd have nowhere to hold all of your data, and SQL queries can become long and cumbersome to type out. Thankfully, CodeIgniter gives us a brilliantly simple way to interact with our Database. The database library also makes changing between database types—from MySQL to Oracle, for example—easier, because it acts as a wrapper and provides many functions for us to use on the database.

In this article by Adam Griffiths, author of CodeIgniter 1.7 Professional Development, you will:

  • Perform database queries with the database library
  • Return query results using the database helper functions
  • Create queries using the active record library
  • Learn how to cache active record queries
  • Modify database tables using database forge

(Read more interesting articles on CodeIgniter 1.7 Professional Development here.)

Loading the library

Loading the Database library is slightly different from loading other libraries. This is because it is large and resides in a different folder, unlike the other libraries.

$this->load->database();

Performing simple queries

Let's dive straight in by starting with the simple stuff. CodeIgniter gives us a function that we can pass a SQL Query to, and the query will be run on the database. Here's how it works:

$this->db->query('PUT YOUR SQL HERE');

This function is incredibly simple to use; you simply use this function in place of any native PHP functions you would use to run queries. This function will return TRUE or FALSE for write queries, and will return a dataset for read queries.

There is another function that you can use for very simple queries; this will only return TRUE or FALSE. It won't let you cache your query or run the query timer. In most cases you won't want to use this function.

$this->db->simple_query('PUT YOUR SQL HERE');

The SQL code that you pass to these functions are database-dependent. Only Active Record queries are independent of any type of Database SQL.

Returning values

You can assign the function $this->db->query() to a variable. You can then run a number of helper functions on the variable in order to return the data in different formats. Take the following example:

$query = $this->db->query('SELECT * FROM 'users'');

Return a result object

In this case, returning the result will return an array of objects, or an empty array if the query failed. You would usually use this function in a foreach loop.

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

If your query does not return a result, the CodeIgniter User Guide encourages you to check for a failure before using this function.

if($query->num_rows > 0)
{
foreach($query->result() as $row)
{
echo $row->username;
echo $row->email;
}
}

Returning a result array

You are also able to return the result dataset as an array. Typically, you would use this function inside a foreach loop as well.

foreach($query->result_array() as $row)
{
echo $row['username'];
echo $row['email'];
}

Returning a row object

If your query is only expected to return a single result, you should return the row by using the following function. The row is returned as an object.

if($query->num_rows() > 0)
{
$row = $query->row();
echo $row->username;
echo $row->email;
}

You can return a specific row by passing the row number as a digit in the first parameter.

$query->row(2);

Returning a row array

You can return a row as an array, if you prefer. The function is used in the same way as the previous example.

if($query->num_rows() > 0)
{
$row = $query->row_array();
echo $row['username'];
echo $row['email'];
}

You can return a numbered row by passing the digit to the first parameter, also.

$query->row_array(2);

Result helper functions

Besides the helper function that helps to return the dataset in different ways, there are some other more generalized helper functions.

Number of rows returned

Used in the same way as the other helper functions, this will return the total number of rows returned from a query. Take the following example:

echo $query->num_rows();

Number of fields returned

Just like the previous function, this will return the number of fields returned by your query.

echo $query->num_fields();

Free result

This function will remove the resource ID associated with your query, and free the associated memory. PHP will usually do this by default, although when using many queries you may wish to use this to free up memory space.

$query->free_result();

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

superrrrr......... by
superrrrr.........
Quite helpfull by
I had problems with models until I found this article. Absolutely awesome!!! Thanks, Chris J Kikoti from Dar Es Salaam,Tanzania

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z