blob: b8be809b6b58596050858dde516f7fbadfca6c0c [file] [log] [blame]
James L Parry6ebceb42014-12-04 06:23:14 -08001#################
2Database Metadata
3#################
Derek Jones8ede1a22011-10-05 13:34:52 -05004
James L Parry6ebceb42014-12-04 06:23:14 -08005**************
6Table MetaData
7**************
8
9These functions let you fetch table information.
10
11List the Tables in Your Database
12================================
13
14**$this->db->list_tables();**
15
16Returns an array containing the names of all the tables in the database
17you are currently connected to. Example::
18
19 $tables = $this->db->list_tables();
20
21 foreach ($tables as $table)
22 {
23 echo $table;
24 }
25
26
27Determine If a Table Exists
28===========================
29
30**$this->db->table_exists();**
31
32Sometimes it's helpful to know whether a particular table exists before
33running an operation on it. Returns a boolean TRUE/FALSE. Usage example::
34
35 if ($this->db->table_exists('table_name'))
36 {
37 // some code...
38 }
39
40.. note:: Replace *table_name* with the name of the table you are looking for.
41
42
43**************
44Field MetaData
45**************
46
47List the Fields in a Table
48==========================
49
50**$this->db->list_fields()**
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52Returns an array containing the field names. This query can be called
53two ways:
54
551. You can supply the table name and call it from the $this->db->
56object::
57
Joseph Wensleyf24f4042011-10-06 22:53:29 -040058 $fields = $this->db->list_fields('table_name');
59
60 foreach ($fields as $field)
61 {
62 echo $field;
63 }
Derek Jones8ede1a22011-10-05 13:34:52 -050064
652. You can gather the field names associated with any query you run by
66calling the function from your query result object::
67
Joseph Wensleyf24f4042011-10-06 22:53:29 -040068 $query = $this->db->query('SELECT * FROM some_table');
69
70 foreach ($query->list_fields() as $field)
71 {
72 echo $field;
73 }
Derek Jones8ede1a22011-10-05 13:34:52 -050074
James L Parry6ebceb42014-12-04 06:23:14 -080075
76Determine If a Field is Present in a Table
77==========================================
78
79**$this->db->field_exists()**
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81Sometimes it's helpful to know whether a particular field exists before
82performing an action. Returns a boolean TRUE/FALSE. Usage example::
83
Joseph Wensleyf24f4042011-10-06 22:53:29 -040084 if ($this->db->field_exists('field_name', 'table_name'))
85 {
86 // some code...
87 }
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Joseph Wensleyf24f4042011-10-06 22:53:29 -040089.. note:: Replace *field_name* with the name of the column you are looking
90 for, and replace *table_name* with the name of the table you are
91 looking for.
Derek Jones8ede1a22011-10-05 13:34:52 -050092
James L Parry6ebceb42014-12-04 06:23:14 -080093
94Retrieve Field Metadata
95=======================
96
97**$this->db->field_data()**
Derek Jones8ede1a22011-10-05 13:34:52 -050098
99Returns an array of objects containing field information.
100
101Sometimes it's helpful to gather the field names or other metadata, like
102the column type, max length, etc.
103
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400104.. note:: Not all databases provide meta-data.
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
106Usage example::
107
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400108 $fields = $this->db->field_data('table_name');
109
110 foreach ($fields as $field)
111 {
112 echo $field->name;
113 echo $field->type;
114 echo $field->max_length;
115 echo $field->primary_key;
116 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118If you have run a query already you can use the result object instead of
119supplying the table name::
120
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400121 $query = $this->db->query("YOUR QUERY");
122 $fields = $query->field_data();
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
124The following data is available from this function if supported by your
125database:
126
127- name - column name
128- max_length - maximum length of the column
129- primary_key - 1 if the column is a primary key
James L Parry6ebceb42014-12-04 06:23:14 -0800130- type - the type of the column