如何通过使用带有Codeigniter的MYSQL来降低表的返回值?

时间:2016-02-18 16:50:17

标签: php mysql codeigniter

我尝试使用CI3.0.4中的方法为此过程创建表格我希望在表格放置创建后检查返回值,是否设置 foreing key

当我尝试删除表但该表不存在于数据库中时,此函数只能检查drop是否为真我的 drop()仍然返回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;
        }

    }
}

?>

感谢您的帮助

1 个答案:

答案 0 :(得分: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;
    }
}

我希望它为你工作。