PHP静态self::/$this->;查询数据库时的计时

PHP静态self::/$this->;查询数据库时的计时,php,mysql,scope,Php,Mysql,Scope,我最近在做一点数据库维护,目标是对某些事情进行一点错误检查和保护,我可以列出表并将它们存储在一个数组中而不会出现问题,但是当我尝试验证该表上的字段时,我的问题出现了。。。它确实有效——但在第二次询问时。 我是否错过了什么,或者这是PHP内部的时间问题 简单表格创建: CREATE TABLE `test_table` ( `testfield1` int(11) NOT NULL AUTO_INCREMENT, `testfield2` int(11) NULL, `testfiel

我最近在做一点数据库维护,目标是对某些事情进行一点错误检查和保护,我可以列出表并将它们存储在一个数组中而不会出现问题,但是当我尝试验证该表上的字段时,我的问题出现了。。。它确实有效——但在第二次询问时。 我是否错过了什么,或者这是PHP内部的时间问题

简单表格创建:

CREATE TABLE `test_table` (
  `testfield1` int(11) NOT NULL AUTO_INCREMENT,
  `testfield2` int(11) NULL,
  `testfield3` int(11) NULL,
  `testfield4` int(11) NULL,
  PRIMARY KEY (`testfield1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
<?php
include_once("config.php");

class dbController {

    static $dbTables;
    static $curTable;
    static $dbTableFields;

    protected $dbh;

    function __construct() {
        // DB_SERVER, DB_NAME, DB_USER + DB_PASS login credentials 
        // defined in config.php.
        $this->dbh = new PDO(
            "mysql:host=". DB_SERVER .";dbname=" . DB_NAME,
            DB_USER,
            DB_PASS,
            array(PDO::ATTR_PERSISTENT => true)
        );

        // List the tables on the Database.
        $sth = $this->dbh->query("SHOW TABLES");
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        foreach($result as $table) {
            self::$dbTables[] = $table['Tables_in_' . DB_NAME];
        }
    }

    // Check field exists in table.
    function check_fields($table, $field) {

        if (in_array($table, self::$dbTables)) {
            if (self::$curTable != $table) {
                self::$curTable = $table;
                $sth = $this->dbh->query("SHOW COLUMNS FROM `$table`");
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
                foreach ($result as $field) {
                    self::$dbTableFields[] = $field['Field'];
                }
            }
            return in_array($field, self::$dbTableFields) 
                ? "true<br />" : "false<br />";
        }
    }
}
false
true
true
精简PHP代码:

CREATE TABLE `test_table` (
  `testfield1` int(11) NOT NULL AUTO_INCREMENT,
  `testfield2` int(11) NULL,
  `testfield3` int(11) NULL,
  `testfield4` int(11) NULL,
  PRIMARY KEY (`testfield1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
<?php
include_once("config.php");

class dbController {

    static $dbTables;
    static $curTable;
    static $dbTableFields;

    protected $dbh;

    function __construct() {
        // DB_SERVER, DB_NAME, DB_USER + DB_PASS login credentials 
        // defined in config.php.
        $this->dbh = new PDO(
            "mysql:host=". DB_SERVER .";dbname=" . DB_NAME,
            DB_USER,
            DB_PASS,
            array(PDO::ATTR_PERSISTENT => true)
        );

        // List the tables on the Database.
        $sth = $this->dbh->query("SHOW TABLES");
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        foreach($result as $table) {
            self::$dbTables[] = $table['Tables_in_' . DB_NAME];
        }
    }

    // Check field exists in table.
    function check_fields($table, $field) {

        if (in_array($table, self::$dbTables)) {
            if (self::$curTable != $table) {
                self::$curTable = $table;
                $sth = $this->dbh->query("SHOW COLUMNS FROM `$table`");
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
                foreach ($result as $field) {
                    self::$dbTableFields[] = $field['Field'];
                }
            }
            return in_array($field, self::$dbTableFields) 
                ? "true<br />" : "false<br />";
        }
    }
}
false
true
true
结果:

CREATE TABLE `test_table` (
  `testfield1` int(11) NOT NULL AUTO_INCREMENT,
  `testfield2` int(11) NULL,
  `testfield3` int(11) NULL,
  `testfield4` int(11) NULL,
  PRIMARY KEY (`testfield1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
<?php
include_once("config.php");

class dbController {

    static $dbTables;
    static $curTable;
    static $dbTableFields;

    protected $dbh;

    function __construct() {
        // DB_SERVER, DB_NAME, DB_USER + DB_PASS login credentials 
        // defined in config.php.
        $this->dbh = new PDO(
            "mysql:host=". DB_SERVER .";dbname=" . DB_NAME,
            DB_USER,
            DB_PASS,
            array(PDO::ATTR_PERSISTENT => true)
        );

        // List the tables on the Database.
        $sth = $this->dbh->query("SHOW TABLES");
        $result = $sth->fetchAll(PDO::FETCH_ASSOC);
        foreach($result as $table) {
            self::$dbTables[] = $table['Tables_in_' . DB_NAME];
        }
    }

    // Check field exists in table.
    function check_fields($table, $field) {

        if (in_array($table, self::$dbTables)) {
            if (self::$curTable != $table) {
                self::$curTable = $table;
                $sth = $this->dbh->query("SHOW COLUMNS FROM `$table`");
                $result = $sth->fetchAll(PDO::FETCH_ASSOC);
                foreach ($result as $field) {
                    self::$dbTableFields[] = $field['Field'];
                }
            }
            return in_array($field, self::$dbTableFields) 
                ? "true<br />" : "false<br />";
        }
    }
}
false
true
true

我尝试使用$this->调用将静态变量替换为公共变量,但仍然得到相同的结果。我是否遗漏了什么或这是一个错误?

问题是,您在第一次调用函数时覆盖了
$field
变量:

function check_fields($table, $field) {
  ...
  foreach ($result as $field) {
                      ^^^^^^
在循环的末尾,
$field
包含一个数组,该数组使用的是最后一个值,而不是您期望的字符串

第二次使用相同的表名调用函数时,将跳过该部分,作为
self::$curTable====$table

只需更改循环中变量的名称:

foreach ($result as $i) {
   self::$dbTableFields[] = $i['Field'];
}

问题在于,您在第一次调用函数时覆盖了
$字段
变量:

function check_fields($table, $field) {
  ...
  foreach ($result as $field) {
                      ^^^^^^
在循环的末尾,
$field
包含一个数组,该数组使用的是最后一个值,而不是您期望的字符串

第二次使用相同的表名调用函数时,将跳过该部分,作为
self::$curTable====$table

只需更改循环中变量的名称:

foreach ($result as $i) {
   self::$dbTableFields[] = $i['Field'];
}

检查构造函数末尾的
self::$dbTables
中的内容。
echo”“;打印(数据库控制器::$dbTables);回声“
生成数据库上的表数组。检查构造函数最末端的
self::$dbTables
中的内容。
echo”“;打印(数据库控制器::$dbTables);回声“在数据库上生成一个表数组。我刚刚完成了在本地主机上设置数据库和脚本的工作,开始调试-抱怨我知道这将是我错过的非常简单(或愚蠢)的事情,谢谢!)@卢卡斯,不客气。如果它对您有效,请不要忘记将答案标记为已接受(计票下方的复选标记)。抱歉,我对这一点很陌生,因此现在应该将其列为已回答,即使我确实觉得遗漏了一些如此明显的内容有点傻——也就是说,如果再次发生,我会考虑将来以这种方式看待它!我刚刚完成了在本地主机上设置数据库和脚本以开始调试的工作-抱怨我知道这将是我错过的非常简单(或愚蠢)的事情,谢谢!)@卢卡斯,不客气。如果它对您有效,请不要忘记将答案标记为已接受(计票下方的复选标记)。抱歉,我对这一点很陌生,因此现在应该将其列为已回答,即使我确实觉得遗漏了一些如此明显的内容有点傻——也就是说,如果再次发生,我会考虑将来以这种方式看待它!