I have two class files: one is called class.database.php, and it is used solely for any functions that have to be done on the database (connect, disconnect, query, etc.)
This is class.database.php:
<?php
class DATABASE
{
public function __construct() {
$this->getConnected();
}
public function getConnected() {
$dbHost = "localhost";
$dbUser = "tysonmoyes";
$dbPassword = "F!lmtrepid";
$db = "tysonmoyes";
$dbConn = new mysqli($dbHost, $dbUser, $dbPassword, $db);
$this->dbConn = $dbConn;
}
function queryDB($queryString) {
return mysqli_query($this->getConnected(), $queryString);
}
public function close() {
mysqli_close($this->connection);
}
}
?>
My second class file is called class.users.php, and it handles all the information on a user account. It looks like this:
<?php
include_once('config.php');
class USER
{
private $conn;
// All the variables needed for the user profile.
public $username;
public $userID;
public $password;
public $firstName;
public $lastName;
public $emailAddress;
public $address;
public $city;
public $province;
public $country;
var $myConn;
function __construct($conn){
$this->myConn = $conn;
}
function createNewUser($username, $password) {
// Clean inputs
$username = trim($username);
$password = trim($password);
// Encrypt password
$password = md5($password);
// Check if username already exists
$checkSQL = "SELECT * FROM users WHERE username = '$username'";
$checkResult = $this->myConn->queryDB($checkSQL);
if($checkResult->num_rows > 0) {
$error = "true";
$errorMessage = "This username has already been taken. Please try again";
}
else {
$insertSQL = "INSERT INTO users(username, password) VALUES('$username', '$password')";
//$insertResult = $this->callDB()->query($insertSQL);
// Get the user ID
$userID = $this->insert_id;
// Set the SESSION globals
$_SESSION['username'] = $username;
$_SESSION['userID'] = $userID;
}
}
function login($username, $password) {
$sql = "SELECT * FROM users WHERE username = '$username' && password = '$password'";
$result = $this->conn->query($sql);
$row = $this->conn->fetch_array($result, MYSQL_ASSOC);
$count = $this->conn->num_rows($result);
if ($count == 1) {
// Set Session Variables
$_SESSION['username'] = $username;
$_SESSION['userID'] = $row['userID'];
return true;
}
}
function isLoggedIn() {
if(isset($_SESSION['username'])) {
return true;
}
else {
return false;
}
}
function redirect($url) {
header("Location: $url");
}
function logout() {
session_destroy();
unset($_SESSION['username']);
}
}
?>
As you can see, the class.user.php calls a "config.php" file, which simply creates a new DATABASE and a new USER, using a link created from making a new DATABASE:
<?php
// Turn on all error reporting
ERROR_REPORTING(E_ALL);
ini_set('display_errors', 1);
// Start Session
session_start();
// Set error to false, and blank error message
$error = "false";
$errorMessage = "";
// Include Database info
require_once('class.database.php');
$link = new DATABASE();
// Include User info
require_once('class.user.php');
// Create instance for user class
$activeUser = new USER($link);
?>
Now, I'd like to focus on my queries, because none of them are working, and I understand why. The query function is in the DATABASE class, but $this is pointing to the USER class.
My question is: How should I write my query so that it properly calls the DATABASE class.
Also, before anyone mentions it, yes, I know md5 is a no-no, but this is for a class project that will be using mock user data, and our professor said that md5 was sufficient encryption for this project
EDIT: For the sake of this, could we focus on the createNewUser function in class.user.php? That's the part I'm currently playing with.
class USER extends DATABASEin that case you don't need this// Include Database info require_once('class.database.php'); $link = new DATABASE();$this->myConnnot$this->connfrom what I can see.$this->dbConnneeds to be returned in yourgetConnected()method. This is likely the main culprit as to why your connection doesn't work.