Problem: I have 2 classes, DB class and a User class, that will work if placed in the same class but when i seperate them i cant figure out how to get the user class to connect using the DB class.
I have ‘DBinterface’ class and a ‘user’ class.
***dbinterface.php
<? class dbinterface {
var $dbHost,
$dbUser,
$dbName,
$dbPass,
$dbUserTable;
function User() {
$this->dbHost = 'host':
$this->dbUser = 'user';
$this->dbName = 'name';
$this->dbPass = 'pass';
$this->dbUserTable = 'table';
}
} // End dbinterface class definition ?>
***user.php
<?
include('dbinterface.php');
class User {
var $userID,
$userName,
$userPassword;
function registerUser($userName, $userPassword) {
// Connect to database
$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
if(!$dbLink) die("Could not connect to database. " . mysql_error());
// Select database
mysql_select_db($this->dbName);
// Insert data
$query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
$result = mysql_query($query);
// Test to make sure query worked
if(!$result) die("Query didn't work. " . mysql_error());
// Get the user ID
$this->userID = mysql_insert_id();
// Close database connection
mysql_close($dbLink);
// Assign the values to the data members
$this->userName = $userName;
$this->userPassword = $userPassword;
} // End registerUser() ?>
PS:I have removed security and other mumbojumbo for easy legibility.
any and all help is MUCH appriciated!
Well. I’ll try to imagine what are you wanted to do 🙂
You need one class where you will store db variables, such as username, password and so on and 2nd class which will work with db-class params.
/* file1 */
/* file2 */
/* file3 */
This is fast solution. You should learn how to create classes and project architecture. Use php.net for more information. Read about Object-oriented programming. And read about SQL-injections for safer code.