Question

I'm getting this PHP Fatal Error:

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

connect.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();
}?>
Was it helpful?

Solution

Try preparing the query instead:

$stmt = $db->prepare($members_table_stmt);
if($stmt->execute()){
   //success!
}else{
   //fail
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top