table_exists()方法可能无法正常工作 [英] table_exists() method might not be working properly
问题描述
我开始写一个简单的Model类使用codeigniter 2.1.0。所有我想要它现在做的是创建和删除它代表的MySQL表。这里是代码:
<?php
类Users_model扩展CI_Model {
public function createTable(){
if($ this-> db-> table_exists('users')== FALSE){
$ query = CREATE TABLE users(
id SERIAL PRIMARY KEY,
email VARCHAR(52)NOT NULL check(email<>''),
UNIQUE(email)
)ENGINE = InnoDB;;
$ this-> db-> query($ query);
}
}
public function deleteTable(){
if($ this-> db-> ; table_exists('users')){
$ query =DROP TABLE users;;
$ this-> db-> query($ query);
} else {
echousers not found< br />;
$ tables = $ this-> db-> list_tables();
foreach($ tables as $ table)
{
echo $ table。< br />;
}
}
}
}
?>
这是我在主类中执行的代码:
$ this-> load-> model('users_model');
$ this-> users_model-> deleteTable();
$ this-> users_model-> createTable();
第一次跑步是罚款和dandy。表创建正常。但是,当我再次运行它,我得到的输出:
未找到用户
用户
进一步检查后,我意识到 table_exists()
c $ c> TRUE 。 var_dump($ this-> db-> table_exists('users'));
返回 bool(false)
。
那么,我做错了什么?这很简单,只需从文档复制粘贴即可。 Google不返回任何相关的东西,作为一个C程序员,我的心态总是如果有一个错误,这是你的。(理查德·斯托曼从来没有错过)但是,因为斯塔尔曼先生与这一点没有什么,一个可能性。
TL; DR
为什么 table_exists('users' 在上面的代码中不会返回
TRUE
即使 list_tables ;
尝试使用 var_dump()
,而不是 echo
。布尔值不适用于 echo
。
var_dump this-> db-> table_exists('users'));
源代码的 table_exists
如下所示:
函数table_exists($ table_name)
{
return(!in_array($ this-> _protect_identifiers($ table_name,TRUE,FALSE, FALSE),$ this-> list_tables()))? FALSE:TRUE;
}
从评论更新
$ b
如果你看看 _protect_identifiers 它会将数据库名称添加到表名称。但应该有一个。 db.table
之间。也许你的数据库配置搞乱了?
I'm starting to write a simple Model class using codeigniter 2.1.0. All that I want it to do for now is create and delete the MySQL table it represents. Here's the code:
<?php
class Users_model extends CI_Model {
public function createTable(){
if( $this->db->table_exists('users') == FALSE ){
$query = "CREATE TABLE users(
id SERIAL PRIMARY KEY,
email VARCHAR(52) NOT NULL check(email <> ''),
UNIQUE (email)
)ENGINE = InnoDB;";
$this->db->query($query);
}
}
public function deleteTable(){
if( $this->db->table_exists('users') ){
$query = "DROP TABLE users;";
$this->db->query($query);
} else {
echo "users not found <br />";
$tables = $this->db->list_tables();
foreach ($tables as $table)
{
echo $table."<br />";
}
}
}
}
?>
And this is the code I'm executing in the main class:
$this->load->model('users_model');
$this->users_model->deleteTable();
$this->users_model->createTable();
The first run is fine and dandy. The tables are created just fine. But then, when I run it again, I get the output:
users not found
users
Upon further inspection I realize table_exists()
never returns TRUE
. var_dump($this->db->table_exists('users'));
returns bool(false)
.
So, am I doing something wrong? This is as simple as it gets, copy pasted from the documentation. Google doesn't return anything related, and being a C programmer my mindset is always "If there's a bug, it's yours. (Richard Stallman is never wrong)"... But since Mr. Stallman has little to do with this, there's a possibility.
TL;DR
Why does table_exists('users')
in the above code never returns TRUE
even if list_tables()
does return 'users';
Try using var_dump()
instead of echo
. Boolean values don't work well with echo
.
var_dump( $this->db->table_exists('users') );
The source code of table_exists
looks like this:
function table_exists($table_name)
{
return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
}
Update from comments
If you have a look at _protect_identifiers it does add the database name to the table name. But there should be a . between db.table
. Maybe your db config is messed up?
这篇关于table_exists()方法可能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!