2016-02-18 40 views
0

我試圖創建一個使用方法CI3.0.4這個過程中,我希望有一個表後檢查返回值下降創建表,設置foreing關鍵與否?如何通過使用MYSQL和Codeigniter刪除表時返回值?

此功能只能檢查時,當我嘗試刪除表,但該表沒有在數據庫中已有我降()仍然返回true同

<?php 

if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 

class Dbforges extends Main_Controller 
{ 
    public $respond = array(); 
    protected $column = ''; 

    public function __construct() 
    { 

     parent::__construct(); 

     $data = array(
      "fid" => "bigint(50) AUTO_INCREMENT,", 
      "fk_c_id" => "bigint(50),", 
      "fk_group_id" => "bigint(50),", 
      "fk_user_id" => "bigint(50),", 
      "fk_product_id" => " bigint(50),", 
      "feaddata" => " decimal(10,2) NOT NULL,", 
      "credit" => "decimal(10,2) NOT NULL,", 
      "b_debit" => "decimal(10,2) NOT NULL,", 
      "b_credit" => "decimal(10,2) NOT NULL,", 
      "description" => " varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,", 
      "PRIMARY KEY" => "(`fid`),", 
      "KEY `fk_c_id` " => "(`fk_c_id`),", 
      "KEY `fk_group_id` " => "(`fk_group_id`),", 
      "KEY `fk_user_id` " => "(`fk_user_id`),", 
      "KEY `fk_product_id` " => "(`fk_product_id`)" 
     ); 

     if ($this->drop("feadback") == false) { 
      $this->respond[] = 'nod'; 
     } else { 
      $this->respond[] = 'd'; 
     } 
     if ($this->AddTables("feadback", $data) == false) { 
      $this->respond[] = 'noc'; 
     } else { 
      $this->respond[] = 'c'; 
     } 
     if ($this->Add_Foreignkey("feadback", "fk_c_id", "cat", "c_id") == false) { 
      $this->respond[] = 'nfk'; 
     } else { 
      $this->respond[] = 'fk'; 
     } 
     echo json_encode(array("res"=>$this->respond)); 
    } 

    public function index() 
    { 

    } 

    public function AddTables($table, $data) 
    { 

     if (!empty($table) && !empty($data)) { 

      foreach ($data as $k => $col) { 
       $this->column .= $k . ' ' . $col; 
      } 
      if ($this->db->query("CREATE TABLE IF NOT EXISTS $table ($this->column) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT= 0;") == true) { 
       return true; 
      } else { 
       return false; 
      } 
     } 
    } 

    protected function drop($table = false) 
    { 
     if ($table) { 
      if ($this->db->query("DROP TABLE IF EXISTS $table;") == true) ; 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public function Add_Foreignkey($fkTable, $fk, $refTables, $referal) 
    { 
     if ($this->db->query("ALTER TABLE $fkTable ADD CONSTRAINT $fkTable.$fk FOREIGN KEY ($fk) REFERENCES `$refTables` ($referal) ON DELETE CASCADE ON UPDATE CASCADE;") == true) { 
      return true; 
     } else { 
      return false; 
     } 

    } 
} 

?> 

感謝您的幫助

下降是真實的

回答

0

試試這個

public function drop($table = false) 
{ 
    if ($table) { 
     if ($this->db->query("DROP TABLE IF EXISTS $table;") == true){ 
      $query = $this->db->query("SHOW TABLES LIKE '".$table."'"); 
      if($query->num_rows() != null) 
      { 
       return true; 
      } 
      else{ 
       return false; 
      } 
     } 
    } 
    else 
    { 
     return false; 
    } 
} 

我希望它爲你工作。

+0

嗨,朋友我已經使用但不工作 –

+0

首先檢查什麼是這個$ this-> db-> query(「DROP TABLE IF EXISTS $ table;」);聲明是回報? @HengSopheak –

+0

它告訴我真正的人的時間 –