简体   繁体   English

对MySQL的查询返回null,然后才能正常工作

[英]A query to MySQL returns null and before it used to work just fine

Basically I want to create a query using a php PDO to check if the table "page" exists in my db "test". 基本上我想用php PDO创建一个查询来检查我的db“test”中是否存在表“page”。 I didn't know how to do it and I got some help here . 我不知道怎么做,我在这里得到了一些帮助。

My code worked perfect ... Until now that I made everything go in classes... and now the var_dump($r2) returns NULL and I don't know what's wrong with the code. 我的代码工作得很完美......直到现在我把所有东西都放到了类中......现在var_dump($r2)返回NULL ,我不知道代码有什么问题。 I didnt change anything other than putting this into OOP... 除了把它放到OOP之外,我没有改变任何东西......

Can anyone spot the problem?? 谁能发现问题? because I cant see it. 因为我无法看到它。 Thx u 谢谢你

$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";

    }

The full class is the following one 完整的课程如下

    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;
    }


}

    }

The biggest problem that I can see is you are not creating $db - only inside the construct. 我能看到的最大问题是你没有创建$ db - 只在构造内部。 Try adding to this section: 尝试添加到此部分:

class phase2 {

    function __construct () {

Adding this statement public $db; 添加此语句public $db; :

class phase2 {

    public $db;

    function __construct () {

Unless I am mistaken, you can't cast a variable from within a method without declaring it first. 除非我弄错了,否则你不能在没有首先声明它的情况下从方法中转换变量。 You'd need to do the same for any other variables you need to access from other methods in that class. 对于需要从该类中的其他方法访问的任何其他变量,您需要执行相同的操作。 Take a read of the basics: http://www.php.net/manual/en/language.oop5.basic.php 阅读基础知识: http//www.php.net/manual/en/language.oop5.basic.php

Also, I'd suggest turning on error reporting. 另外,我建议打开错误报告。

Your query is trying to find a table named page , however, your CREATE TABLE creates a table named pages : 您的查询正在尝试查找名为page的表,但是,您的CREATE TABLE会创建一个名为pages的表:

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

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

Unless you actually have both tables, the error lies in one of those two places. 除非你实际上有两个表,否则错误在于这两个地方之一。

You are using require_once() in your constructor. 您在构造函数中使用require_once()。 The class will only initialize correctly the first time. 该类只会在第一次正确初始化。 After that the config won't load so the variables aren't set. 之后,配置将不会加载,因此未设置变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM