[英]A query to MySQL returns null and before it used to work just fine
基本上我想用php PDO創建一個查詢來檢查我的db“test”中是否存在表“page”。 我不知道怎么做,我在這里得到了一些幫助。
我的代碼工作得很完美......直到現在我把所有東西都放到了類中......現在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;
}
}
}
我能看到的最大問題是你沒有創建$ db - 只在構造內部。 嘗試添加到此部分:
class phase2 {
function __construct () {
添加此語句public $db;
:
class phase2 {
public $db;
function __construct () {
除非我弄錯了,否則你不能在沒有首先聲明它的情況下從方法中轉換變量。 對於需要從該類中的其他方法訪問的任何其他變量,您需要執行相同的操作。 閱讀基礎知識: http : //www.php.net/manual/en/language.oop5.basic.php
另外,我建議打開錯誤報告。
您的查詢正在嘗試查找名為page
的表,但是,您的CREATE TABLE
會創建一個名為pages
的表:
$IDB1 = $this->db->prepare(
"CREATE TABLE pages ("
...
$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');
除非你實際上有兩個表,否則錯誤在於這兩個地方之一。
您在構造函數中使用require_once()。 該類只會在第一次正確初始化。 之后,配置將不會加載,因此未設置變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.