I need to get the table structure of my database tables. SO i am using the below code.
class Some_model extends CI_Model{
public $DB1;
function __construct(){
parent::__construct();
$this->DB1 = $this->load->database('default',TRUE);
}
function getTableStructure($tableName){
echo $tableName; //this Outputs my table Name that i pass in the function
return $this->DB1->field_data($tableName) ; //this gives error for some tables
}
}
I get a Database Error
To fetch fields requires the name of the table as a parameter.
Note : This function works on some tables but i get this error on few other tables. Table i am checking on is "admin_user"
Update :
I have checked the field_data function in the DB_driver.php file in the system/database folder.
when i print the return object i.e
echo "<pre">;print_r($query->field_data());die();
//return $query->field_data(); commented this line print's the object
However,
//echo "<pre">;print_r($query->field_data());die(); comment this line shows error.
return $query->field_data();
I am not sure why don't you use default DB configuration by calling
$this->db->field_data($tableName);
instead custom variable DB1 since you don't change connection, db or anything shown or mentioined in question. However, next code would work like a charm:
public function desc1($tableName)
{
$fields = $this->db->field_data($tableName);
var_dump($fields);
}
or even you can use custom query like:
public function desc2($tableName)
{
$fields = $this->db->query("DESCRIBE `" . $tableName . "`");
var_dump($fields->result());
}
Those two functions are slightly different in returned results so you have to check both of them and see what would better suit to your code (i.e. custom one gives extra field too).
Work fine for me .
function getTableStructure($tableName){
if(isset($tableName)){
return $this->db->field_data($tableName) ;
}
}
check! and make sure there is no keyword used as field name in your table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With