James L Parry | 6ebceb4 | 2014-12-04 06:23:14 -0800 | [diff] [blame] | 1 | ################# |
| 2 | Database Metadata |
| 3 | ################# |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 4 | |
James L Parry | 6ebceb4 | 2014-12-04 06:23:14 -0800 | [diff] [blame] | 5 | ************** |
| 6 | Table MetaData |
| 7 | ************** |
| 8 | |
| 9 | These functions let you fetch table information. |
| 10 | |
| 11 | List the Tables in Your Database |
| 12 | ================================ |
| 13 | |
| 14 | **$this->db->list_tables();** |
| 15 | |
| 16 | Returns an array containing the names of all the tables in the database |
| 17 | you 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 | |
| 27 | Determine If a Table Exists |
| 28 | =========================== |
| 29 | |
| 30 | **$this->db->table_exists();** |
| 31 | |
| 32 | Sometimes it's helpful to know whether a particular table exists before |
| 33 | running 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 | ************** |
| 44 | Field MetaData |
| 45 | ************** |
| 46 | |
| 47 | List the Fields in a Table |
| 48 | ========================== |
| 49 | |
| 50 | **$this->db->list_fields()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
| 52 | Returns an array containing the field names. This query can be called |
| 53 | two ways: |
| 54 | |
| 55 | 1. You can supply the table name and call it from the $this->db-> |
| 56 | object:: |
| 57 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 58 | $fields = $this->db->list_fields('table_name'); |
| 59 | |
| 60 | foreach ($fields as $field) |
| 61 | { |
| 62 | echo $field; |
| 63 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
| 65 | 2. You can gather the field names associated with any query you run by |
| 66 | calling the function from your query result object:: |
| 67 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 68 | $query = $this->db->query('SELECT * FROM some_table'); |
| 69 | |
| 70 | foreach ($query->list_fields() as $field) |
| 71 | { |
| 72 | echo $field; |
| 73 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
James L Parry | 6ebceb4 | 2014-12-04 06:23:14 -0800 | [diff] [blame] | 75 | |
| 76 | Determine If a Field is Present in a Table |
| 77 | ========================================== |
| 78 | |
| 79 | **$this->db->field_exists()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
| 81 | Sometimes it's helpful to know whether a particular field exists before |
| 82 | performing an action. Returns a boolean TRUE/FALSE. Usage example:: |
| 83 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 84 | if ($this->db->field_exists('field_name', 'table_name')) |
| 85 | { |
| 86 | // some code... |
| 87 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 89 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
James L Parry | 6ebceb4 | 2014-12-04 06:23:14 -0800 | [diff] [blame] | 93 | |
| 94 | Retrieve Field Metadata |
| 95 | ======================= |
| 96 | |
| 97 | **$this->db->field_data()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
| 99 | Returns an array of objects containing field information. |
| 100 | |
| 101 | Sometimes it's helpful to gather the field names or other metadata, like |
| 102 | the column type, max length, etc. |
| 103 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 104 | .. note:: Not all databases provide meta-data. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
| 106 | Usage example:: |
| 107 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 108 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | |
| 118 | If you have run a query already you can use the result object instead of |
| 119 | supplying the table name:: |
| 120 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 121 | $query = $this->db->query("YOUR QUERY"); |
| 122 | $fields = $query->field_data(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
| 124 | The following data is available from this function if supported by your |
| 125 | database: |
| 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 Parry | 6ebceb4 | 2014-12-04 06:23:14 -0800 | [diff] [blame] | 130 | - type - the type of the column |