summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/database/helpers.rst
blob: 2d997a9e0d3f39eb091cb6c2a72f83f2c315f18a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
####################
Query Helper Methods
####################

Information From Executing a Query
==================================

**$this->db->insert_id()**

The insert ID number when performing database inserts.

.. note:: If using the PDO driver with PostgreSQL, or using the Interbase
	driver, this function requires a $name parameter, which specifies the 
	appropriate sequence to check for the insert id.

**$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->last_query()**

Returns the last query that was run (the query string, not the result).
Example::

	$str = $this->db->last_query();
	
	// Produces:  SELECT * FROM sometable....


.. note:: Disabling the **save_queries** setting in your database
	configuration will render this function useless.

Information About Your Database
===============================

**$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

**$this->db->platform()**

Outputs the database platform you are running (MySQL, MS SQL, Postgres,
etc...)::

	echo $this->db->platform();

**$this->db->version()**

Outputs the database version you are running::

	echo $this->db->version();

Making Your Queries Easier
==========================

**$this->db->insert_string()**

This function simplifies the process of writing database inserts. It
returns a correctly formatted SQL insert string. 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')

.. note:: Values are automatically escaped, producing safer queries.

**$this->db->update_string()**

This function simplifies the process of writing database updates. It
returns a correctly formatted SQL update string. 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'

.. note:: Values are automatically escaped, producing safer queries.