我得到這個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();
}?>
這是相當自我解釋。如果($ db-> query($ members_table_stmt)成員函數'query'不能在'$ db'上實例化,因爲這個變量是'empty'。 – Ohgodwhy
所以對不起,我不想粗魯這行:if )){ – user3558634
我是這個領域的新手,你能給我一個解決方案謝謝! – user3558634