2011-08-27 63 views
0

我在笨以下庫,具有以下功能:在問題的笨庫沒有返回值

return $dbs; 

return tables; 

問題getDbs()getTables()是他們不返回什麼我做錯了什麼。難道是因爲我使用的是陣列或某物

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; 
} 


    } 


} 

?> 

這裏是我的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= $this->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(); 
    } 


} 

回答

0

他們返回數組所以使用print_r看到返回什麼。

此外,如果您使用codeignier內置database support,您的模型應該使用這些功能不試圖重新實現它們。