Copy link to clipboard
Copied
I've created a basic PHP login system as a personal project. I've finished all the pages but when I try and submit a username and password I get an error :
Fatal error: Call to a member function prepare() on a non-object in /home/(my username)/public_html/classes/Mysql.php on line 20
I've tinkered with it for a while but I haven't been able to find a solution, can anyone help me out?
<?php
require_once'includes/constants.php';
class Mysql {
private $conn;
function __contruct() {
$this->conn = new mysql (DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or
die('Problem connecting to database');
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM users
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}
Copy link to clipboard
Copied
Can you give us an idea of your file hierarchy?
Apart from that, the first thing I noticed is you're missing ()brackets in your require statement.
Try require_once('includes/constants.php');
Copy link to clipboard
Copied
Sudarshan Thiagarajan wrote:
Apart from that, the first thing I noticed is you're missing ()brackets in your require statement.
Try require_once('includes/constants.php');
That makes no difference. The parentheses are optional with include and require.
There are a couple of problems in the class definition here:
function __contruct() {
$this->conn = new mysql (DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or
die('Problem connecting to database');
}
__construct() is misspelled.
Also it should be new mysqli.
Copy link to clipboard
Copied
That makes no difference. The parentheses are optional with include and require.
Gosh, What was I thinking?! Thanks David for the correction. Appreciate it!
Sorry, Tyharo - if I misguided you.
Copy link to clipboard
Copied
function __contruct() {
$this->conn = new mysql (DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or
die('Problem connecting to database');
}
I changed it to whats shown above but now i get a 503 error saying the server is temorarly busy. I can login into my hosting, view my mysql databases, and login to phpmyadmin just fine; is this on my providers side or is my code causing this error?
Copy link to clipboard
Copied
David mentioned earlier you've misspelled 'construct'. Your code reads 'contruct.