I have the following library in codeIgniter that has functions getDbs()
and getTables()
in the
return $dbs;
and
return tables;
the problem is they don’t return anything what am i doing wrong. Is it because i am using an array or sth
class Db_models {
private $host;
private $user;
private $password;
private $link;
private $dbname;
private $fields;
public $dbs;
//TODO make this flexible especially connect statement
public function __construct($config) {
$this->host = $config[0];
$this->user = $config[1];
$this->password = $config[2];
$this->link = mysql_connect($this->host, $this->user, $this->password);
print_r($this->link);
}
//Get Databases
function getDbs() {
$db_list = mysql_list_dbs($this->link);
$row = mysql_fetch_object($db_list);
var_dump($row);
while ($row = mysql_fetch_object($db_list)) {
$dbs[] = $row->Database;
}
return $dbs;
}
//Get Tables
function getTables($dbname) {
$this->dbname = $dbname;
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
return $tables;
}
//Get Fields
function generateFields($dbname,$table){
mysql_select_db($dbname, $this->link);
$result = mysql_query("SHOW COLUMNS FROM $table");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$this->fields[]=$row;
}
return $this->fields;
}
}
}
?>
Here is my cotroller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*
*/
private $fields=array('s'=>'s');
public function __construct()
{
parent::__construct();
$this->load->library('db_models', $config = array('localhost', 'madawar_Madawar', '23goodboys'));
}
public function index() {
// $this->load->library('db_models', $config = array('localhost', 'Madawar', '23goodboys'));
$data['dbs'] = $this->db_models->getDbs();
$this->load->view('welcome_message', $data);
}
public function database() {
$db_name = $this->input->post('database');
$data['db_name'] = $db_name;
// $this->load->library('db_models', $config = array('localhost', 'Madawar', '23goodboys'));
$data['tables'] = $this->db_models->getTables($db_name);
$this->load->view('database_dash', $data);
}
public function generate($dbname,$table) {
/*$data = decode(TRUE);
print_r($data);
echo "sd";*/
// $this->load->library('db_models', $config = array('localhost', 'Madawar', '23goodboys'));
$data = $this->fields= $thi开发者_Python百科s->db_models->generateFields($dbname,$table);
//print_r($data);
$data=encode($data);
echo $data;
}
public function getFields($some,$data) {
echo $some;
echo $data;
// $this->load->library('db_models', $config = array('localhost', 'Madawar', '23goodboys'));
$this->fields= $this->db_models->getFields();
}
}
They return arrays so use print_r
to see what is returned.
Also if your using codeignier it has database support built in, your model should be using these functions not trying to re-implement them.
精彩评论