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
| Create and Update Queries | |
| Active Record has three functions that help you create new entries in your database. They are $this->db->insert(), $this->db->update(), and $this->db->set(). | |
| The difference between a 'create' and an 'update' query is that when you create a new record, there is no reference to any existing record, you are writing a new one. When you update, there is an existing record, and you are changing it. So in the second case, you have to specify which record you are changing. In both cases, you have to set the values you want to leave in the database after your query. Values you don't set will be left unaltered—or, if they didn't exist before, they will still be 'null' after your query. | |
| CI allows you to set your values either with an array, or with $this->db-set(); the difference is only one of syntax. | |
| So, let's add a line to our sites table in the websites database. We've already connected to this database in our controller. The controller's constructor function included the line: | |
| $this->load- |
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
| Delete Queries | |
| Delete queries are perhaps the simplest to describe. All you need is the name of the table and the ID number of the record to delete. Let's say I want to delete record in my sites table with the ID number 2: | |
| $this->db->where('id', '2'); | |
| $this->db->delete('sites'); | |
| I get slightly nervous around 'delete' queries because they are so powerful. Please remember to make sure that there is a valid value in the 'where' clause, or you may delete your whole table! Neither the author nor Packt Publishing will accept any liability if…. |
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
| Mixing Active Record and 'Classic' Styles | |
| CI doesn't insist that you use Active Record. You can also use CI to issue straight SQL queries. For instance, assuming you loaded the database in your constructor, you can still write queries like this: | |
| $this->db->query("SELECT id, name, url FROM sites WHERE 'type' = 'dynamic'"); | |
| Personally, I find Active Record easier to use. Conceptually, setting out my query in an array makes it easier to see and manipulate as an entity than writing it in SQL syntax. It's slightly more verbose, but clearly structured; it automatically escapes data; and it may be more portable. It also minimizes typing errors with commas | |
| and quotes. | |
| There are a few cases, however, where you may have to resort to the original SQL. You might want to do complex joins, or another example is if you need to use multiple 'where' conditions. If you want to find the websites associated with client 3, but only those of two specific types, you may need to put brackets around the SQL to make sure the query is |
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
| Summary | |
| We've looked at CI's Active Record class and seen how easy it is to: | |
| 1) Set up connections to one or more databases | |
| 2) Do standard SQL read, update, create, and delete queries | |
| 3) Perform other functions that we need, to use a database properly | |
| CI's Active Record function is clean and easy to use, and makes coding much clearer to read. It automates database connections, allowing you to abstract the connection information to one config file. | |
| It can do pretty well anything that you can do with 'classic' SQL—more than I have space to explain here. See the online User Guide for fuller details. |
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
| DROP TABLE IF EXISTS `ci_sessions`; | |
| CREATE TABLE IF NOT EXISTS `ci_sessions` ( | |
| `session_id` varchar(40) NOT NULL default '0', | |
| `peopleid` int(11) NOT NULL, | |
| `ip_address` varchar(16) NOT NULL default '0', | |
| `user_agent` varchar(50) NOT NULL, | |
| `last_activity` int(10) unsigned NOT NULL default '0', | |
| `left` int(11) NOT NULL, | |
| `name` varchar(25) NOT NULL, | |
| `status` tinyint(4) NOT NULL default '0' |
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
| <?php | |
| // USING the OLD MySQL_connect API is an Anti-Pattern .... for reference ONLY! (Anti-Pattern means Bad-Practice) | |
| //echo phpinfo(); | |
| /* -------------------METHOD 1 | |
| $conn=mysql_connect('localhost','root','') or die ('Could NOT Connect'); | |
| mysql_select_db('practice',$conn); |
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
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <p>My mother has <span style="color:blue;font-weight:bold">blue</span> eyes and my father has <span style="color:darkolivegreen;font-weight:bold">dark green</span> eyes.</p> | |
| </body> | |
| </html> | |
| <!-- | |
| Definition and Usage |
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
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <h1>About WWF</h1> | |
| <p>Here is a quote from WWF's website:</p> | |
| <blockquote cite="http://www.worldwildlife.org/who/index.html"> | |
| For 50 years, WWF has been protecting the future of nature. The world’s leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally. | |
| </blockquote> |
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Title of the document</title> | |
| </head> | |
| <body> | |
| The content of the document...... | |
| </body> |