【问题标题】:A query to MySQL returns null and before it used to work just fine对 MySQL 的查询返回 null 并且在它以前可以正常工作之前
【发布时间】:2012-08-29 22:37:30
【问题描述】:

基本上,我想使用 php PDO 创建一个查询来检查我的数据库“test”中是否存在表“page”。我不知道该怎么做,我得到了一些帮助here

我的代码运行良好......直到现在我把所有东西都放在类里......现在var_dump($r2)返回NULL,我不知道代码有什么问题。除了将其放入 OOP 之外,我没有更改任何内容...

谁能发现问题??因为我看不到。 谢谢你

$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');


 // Debbug
    $r2 = $r1->fetchAll;
    var_dump ($r2);

    if (count($r1->fetchAll()) > 0 ) {

        echo "The table PAGE exists";

    }

全班是下面这个

    class phase2 {

        function __construct () {

        $dbFile = 'dbconfig.php';
        $this->dbFile = $dbFile;

        require_once ("$dbFile");   


        $step = $_GET["step"];

        $username = $DB_USER;
        $password = $DB_PASS;
        $server = $DB_SERVER;
        $dbName = $DB_NAME;

        $this->step = $step;
        $this->dbFile = $dbFile;
        $this->username = $username;
        $this->password = $password;
        $this->server = $server;
        $this->dbName = $dbName;

        $db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);

        $this->db = $db;

        if (empty ($_GET['fot']) ) { 

            $fOT = 'false'; 

        } elseif ($_GET['true']) { $fOT = 'true'; }

        $this->fOT = $fOT;

        $this->IDB = $this->handleDatabase( 1 );
        $this->IDB2 = $this->handleDatabase( 2 );
        $this->IDB3 = $this->handleDatabase( 3 );

        }



public function handleDatabase ($num = 1){

// Prepare SQL Statements

    $IDB1 = $this->db->prepare( 
         "CREATE TABLE pages (
          id int(11) NOT NULL auto_increment,
         subject_id int(11) NOT NULL,
          menu_name varchar(30) NOT NULL,
          position int(3) NOT NULL,
          visible tinyint(1) NOT NULL,
          content text NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");

    $IDB2 = $this->db->prepare("
        CREATE TABLE subjects (
          id int(11) NOT NULL auto_increment,
          menu_name varchar(30) NOT NULL,
          position int(3) NOT NULL,
          visible tinyint(1) NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");

    $IDB3 = $this->db->prepare("
        CREATE TABLE users (
          id int(11) NOT NULL auto_increment,
          username varchar(50) NOT NULL,
          hashed_password varchar(40) NOT NULL,
          PRIMARY KEY  (id)
    )ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");

    $name = "IDB".$num;
    return isset( $$name)?$$name:false;

}
//Set Option to True or False

function createTablePages ($fOT){


    $r1 = $this->db->query('SHOW TABLES LIKE \'page\'');

// Debbug
    $r2 = $r1->fetchAll;
    var_dump ($r2);


    if (count($r1->fetchAll()) > 0) {


        echo "The table PAGE exists";

    } elseif ($fOT == 'true') {

            echo "enteres";
            $this->IDB1->execute();
            $this->stepFunction (1,false);

    } 

}
function createTableSubjects ($fOT){

    $r2 = $this->db->query('SHOW TABLES LIKE \'subjects\'');

    if (count($r2->fetchAll()) > 0  && $fOT == 'false') {

            echo "The table SUBJECTS exists ";

    } elseif ($fOT == 'true') {

        $this->IDB2->execute();
        $this->stepFunction (2,false);

    }
}

function createTableUsers ($fOT){

    $r3 = $this->db->query('SHOW TABLES LIKE \'users\'');   

    if (count($r3->fetchAll()) > 0  && $fOT == 'false') {

            echo "The table USERS exists";

    } elseif ($fOT == 'true') {

            $this->IDB3->execute();
            echo "Would you like to populate all the tables?";
    }   
}


public function stepFunction ($fOT,$step){

switch ($step) {

    case 0: 
            $this->createTablePages ($fOT);
            break;
    case 1: 
            $this->createTableSubjects($fOT);
            break;
    case 2: $this->createTableUsers ($fOT);
            break;
    }


}

    }

【问题讨论】:

  • “除了将它放入 OOP 之外,我没有做任何改变……”这是一个重大的改变。
  • 一个完美的例子,像$r1 这样的名字是一种糟糕的做法
  • 你检查数据库连接了吗?

标签: php mysql


【解决方案1】:

您的查询试图查找名为page 的表,但是,您的CREATE TABLE 创建了一个名为pages 的表:

$IDB1 = $this->db->prepare( 
    "CREATE TABLE pages ("
...

$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');

除非您实际上有 两个 表,否则错误出在这两个位置之一。

【讨论】:

  • 谢谢。我的眼睛很累,完全错过了。
【解决方案2】:

我能看到的最大问题是您没有创建 $db - 仅在构造内部。尝试添加到此部分:

class phase2 {

    function __construct () {

添加此语句public $db;:

class phase2 {

    public $db;

    function __construct () {

除非我弄错了,否则你不能在不先声明的情况下从方法中强制转换变量。您需要对需要从该类中的其他方法访问的任何其他变量执行相同的操作。阅读基础知识:http://www.php.net/manual/en/language.oop5.basic.php

另外,我建议打开错误报告。

【讨论】:

  • 现在可以使用了。我只是忘记了它是“页面”而不是“页面”。但我对 __construct 的理解是,它在每次创建类时都声明变量,因此不需要在外部创建它。但我不是在投射变量,而是在投射$this->db,它已在__construct 中分配给$this->db = $db。不知怎么的?
  • 这很奇怪 - 我建议在构造之外声明,这是一种很好的做法,如果其他人也需要查看代码,这将有所帮助。
【解决方案3】:

您在构造函数中使用了 require_once()。该类只会在第一次正确初始化。之后配置将不会加载,因此不会设置变量。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2022-07-06
    • 2017-08-10
    • 1970-01-01
    • 2016-01-14
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多