Skip to content

Instantly share code, notes, and snippets.

@abdullahbutt
Created November 10, 2013 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahbutt/7403165 to your computer and use it in GitHub Desktop.
Save abdullahbutt/7403165 to your computer and use it in GitHub Desktop.
CI db connection
Imagine you are writing a database query. This is how you might write a function within your PHP programme to query a MySQL database:
$connection = mysql_connect("localhost","fred","12345");
mysql_select_db("websites", $connection);
$result = mysql_query ("SELECT * FROM sites", $connection);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
foreach ($row as $attribute)
print "{$attribute[1]} ";
}
Now see how a CI function would handle a similar query:
$this->load->database('websites');
$query = $this->db->get('sites');
foreach ($query->result() as $row)
{
print $row->url
}
Compare the character counts: 244 for the traditional syntax; 112 for CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment