dou6577 2012-08-29 22:37
浏览 84
已采纳

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

Basically I want to create a query using a php PDO to check if the table "page" exists in my db "test". 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. I didnt change anything other than putting this into 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;
    }


}

    }
  • 写回答

3条回答 默认 最新

  • dourong9253 2012-08-29 23:06
    关注

    Your query is trying to find a table named page, however, your CREATE TABLE creates a table named 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记