Created
November 18, 2013 08:27
-
-
Save abdullahbutt/7524496 to your computer and use it in GitHub Desktop.
CI_Connection_DB (establishing connection to db in ci)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In other words, the CI framework is making your code more robust. Now, let's look at how it works. | |
| Firstly, connecting to the database is very simple. In classic PHP, you might say something like this: | |
| $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]} "; | |
| } | |
| In other words, you have to re-state the host, username, and password, make a connection, then select the database from that connection. You have to do this each time. Only then, do you get on to the actual query. CI replaces the connection stuff with one line: | |
| $this->load->database(); | |
| which you put once, in each controller or model or class constructor that you write. After that, in each function within those controllers, etc., you just go straight into your query. The connection information is stored in your database config file, and CI goes and looks it up there each time. | |
| So, in each CI function, you go straight to your query. The query above written in CI comes out as: | |
| $query = $this->db->get('sites'); | |
| foreach ($query->result() as $row) | |
| { | |
| print $row->url | |
| } | |
| Simple, isn't it? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment