Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.

Similar presentations


Presentation on theme: "Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder."— Presentation transcript:

1 Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder. E.g the file its self represent the example. Kindly refer it. You can have groups of connection e.g $db['default']['hostname'] = 'localhost';// this is one of the default group setting variable there are more to be set. You can change define below another array as: $db[‘GroupName']['hostname'];// second Dimension of you group will be same.

2 Connecting to your Database There are two ways to connect to a database: Automatically Connecting The "auto connect" feature will load and instantiate the database class with every page load. To enable "auto connecting", add the word database to the library array, as indicated in the following file: application/config/autoload.php Manually Connecting If only some of your pages require database connectivity you can manually connect to your database by adding this line of code in any function where it is needed, or in your class constructor to make the database available globally in that class. $this->load->database(); For Group $this->load->database(‘groupname’);

3 Quick Example Standard Query With Multiple Results (Object Version) $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->email; } echo 'Total Results: '. $query->num_rows(); Standard Query With Single Result $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row = $query->row(); echo $row->name;

4 Quick Example Standard Query With Multiple Results (Array Version) $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result_array() as $row) { echo $row['title']; echo $row['name']; echo $row['email']; } Standard Query With Single Result (Array version) $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row = $query->row_array(); echo $row['name'];

5 Query(); $this->db->query(); To submit a query, use the following function: $this->db->query('YOUR QUERY HERE'); Query Bindings Bindings enable you to simplify your query syntax by letting the system put the queries together for you. Consider the following example: $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; $this->db->query($sql, array(3, 'live', 'Rick')); The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function.

6 Result Helper Functions $query->num_rows() The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to: $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_rows(); $query->num_fields() The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object: $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_fields();

7 Query Helper Functions $this->db->insert_id() The insert ID number when performing database inserts. $this->db->affected_rows() Displays the number of affected rows, when doing "write" type queries (insert, update, etc.). Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file. $this->db->count_all(); Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example: echo $this->db->count_all('my_table'); \ // Produces an integer, like 25

8 Query Helper Functions $this->db->insert_string(); This function simplifies the process of writing database inserts. Example: $data = array('name' => $name, 'email' => $email, 'url' => $url); $str = $this->db->insert_string('table_name', $data); The first parameter is the table name, The second is an associative array with the data to be inserted. The above example produces: INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')

9 Query Helper Functions $this->db->update_string(); This function simplifies the process of writing database updates. Example: $data = array('name' => $name, 'email' => $email, 'url' => $url); $where = "author_id = 1 AND status = 'active'"; $str = $this->db->update_string('table_name', $data, $where); The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter is the "where" clause. The above example produces: UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'


Download ppt "Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder."

Similar presentations


Ads by Google