how to display the database table names list in co

2019-04-17 10:20发布

问题:

How to display the database table names list in CodeIgniter using given syntax:

$tables=$this->db->query("SHOW TABLES LIKE '%Demo%'");

回答1:

You have to specify the database name.

Check this,

SHOW TABLES FROM `database-name` LIKE '%a%' 

See mysql documentation here

To get table names,

 $tables=$this->db->query("SELECT t.TABLE_NAME AS myTables FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_SCHEMA = 'database name' AND t.TABLE_NAME LIKE '%a%' ")->result_array();    
 foreach($tables as $key => $val) {
      echo $val['myTables']."<br>";// myTables is the alias used in query.
 }


回答2:

You can use this:

$tables = $this->db->list_tables();

foreach ($tables as $table)
{
   echo $table;
}

documentation



回答3:

Try with information_schema.tables

Example:

$query = $this->db->query("SELECT * FROM information_schema.tables WHERE **** ");
$result = $query->result_array();
return $result;

Go through this Chapter 19 INFORMATION_SCHEMA Tables

EDIT 01

$this->db->list_tables();
$this->db->like('name', 'field');