2014-04-22 69 views
0

我得到這個PHP致命錯誤:調用一個成員函數查詢()一個非對象在第25行

Fatal error: Call to a member function query() on a non-object in

C:\xampp\htdocs\University\createTables.php on line 25

createTables.php

<?php 
require_once("connect.php"); 
$members_table_stmt = "CREATE TABLE IF NOT EXISTS members (
    id INT NOT NULL AUTO_INCREMENT, 
    ext_id TEXT, 
    username VARCHAR(16) NOT NULL, 
    email VARCHAR(255) NOT NULL, 
    password TEXT, 
    lastlog DATETIME NOT NULL, 
    signup_date DATETIME NOT NULL, 
    activated ENUM('0','1') NOT NULL DEFAULT '0', 
    avatar VARCHAR(255), 
    banner VARCHAR(255), 
    full_name VARCHAR(255), 
    country VARCHAR(255), 
    state VARCHAR(255), 
    city VARCHAR(255), 
    gender VARCHAR(12), 
    birthday VARCHAR(255), 
    ipaddress VARCHAR(255), 
    PRIMARY KEY (id), 
    UNIQUE KEY username (username,email) 

)"; 
if($db->query($members_table_stmt)){ 
    echo "<h2>Member Table Created Yay!</h2>"; 
}else{ 
    echo "<h2>There was an error creating the members table. Go back and check your code</h2>"; 
} 
$activation_tbl_stmt = "CREATE TABLE IF NOT EXISTS activate (
    id INT NOT NULL AUTO_INCREMENT, 
    user VARCHAR(255) NOT NULL, 
    token VARCHAR(255) NOT NULL, 
    PRIMARY KEY (id) 

)"; 
if($db->query($activation_tbl_stmt)){ 
    echo "<h2>Activation Table Created Yay!</h2>"; 
}else{ 
    echo "<h2>There was an error creating the activation table. Go back and check your code</h2>"; 
}?> 

連接。 php

<?php 
// Usualy "localhost" but could be different on different servers 
$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = ""; 
// Place the name for the MySQL database here 
$db_name = "enseignant"; 

try{ 
$db = new DbConnector(); 
    $db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} 
catch(PDOException $e){ 
    echo $e->getMessage(); 
    exit(); 
}?> 
+0

這是相當自我解釋。如果($ db-> query($ members_table_stmt)成員函數'query'不能在'$ db'上實例化,因爲這個變量是'empty'。 – Ohgodwhy

+0

所以對不起,我不想粗魯這行:if )){ – user3558634

+0

我是這個領域的新手,你能給我一個解決方案謝謝! – user3558634

回答

0

試着準備查詢代替:

$stmt = $db->prepare($members_table_stmt); 
if($stmt->execute()){ 
    //success! 
}else{ 
    //fail 
} 
+0

抱歉我的不好,我不打算在一些論壇上找到這個解決方案,我試過了,甚至沒有$ db = new DbConnector();仍然有相同的問題 – user3558634

+0

看到我的編輯,請避免誤導性的代碼,它只是使它很難幫助您 – meda

+0

所以對不起,好吧,你要我刪除嘗試和趕上,或者你必須把這個代碼放在試試謝謝! – user3558634

相關問題