Multiple Classes... Im New
Posted: Mon Dec 11, 2006 7:26 pm
feyd | Please use
That class could have been written much better, but thats what it is for the time being.
validateuser.php
-Thats not finished, I only got that far until I realized there was a problem.
Im getting mysql errors because the link-id is invalid, because when it was extended, it didnt extend the variables, including the connection variable, so everything crumbles to pieces... I dont know what the solution is. Ive searched for a solution for the past 8 hours, literaly, plus yesterday, there is not one google result that had any code I could look at to see how I could get this to work, and I was really suprised that php class tutorials brought up very very very few actuall tutorials, and none of them did this.
Heres how theyre tied into the script:
Is it possible to extend the variables and connection in the mysql class to the validateduser class? This seems so simple and so absolutely necessary for applications of classes, and yet google has not one result with even a hint at how to do this -_-.
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I havent worked with PHP in over a year, so I was already off to a bad start, and I just started working with it again, and started by working with classes, I never really used them before.
So right now I have 2 classes, mysqldb, and validateduser. mysqldb establishes the connection to the database, and has all the basic mysql methods I need in it. validateduser allows me to get information about the user, check if theyre logged in, if its a valid login, etc-- by using the database.
Thats where the the problem is.
mysql.phpCode: Select all
<?php
class mysqldb
{
var $dbhost;
var $dbusername;
var $dbpassword;
var $dbname;
var $dblink;
function connect() {
$this->dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword);
if (!$this->dblink) {
}
elseif (!mysql_select_db($this->dbname, $this->dblink)) {
$this->dblink = false;
}
return $this->dblink;
}
function safe($x) {
return mysql_real_escape_string($x);
}
function query($run) {
return mysql_query($run, $this->dblink);
}
function fetch($query) {
return mysql_fetch_array($query);
}
function num($query) {
return mysql_num_rows($query);
}
function insert($table, $fieldnames, $fieldvalues) {
return $this->query("INSERT INTO $table ($fieldnames) VALUES ($fieldvalues)");
}
function delete($table, $fieldname, $fieldmatch) {
return $this->query("DELETE FROM $table WHERE $fieldname = '$fieldmatch'");
}
function update($table, $set, $where) {
return $this->query("UPDATE `$table` SET $set WHERE $where");
}
function lastinsertid() {
return mysql_insert_id;
}
function close() {
return mysql_close($this->dblink);
}
}
?>validateuser.php
Code: Select all
<?php
class validateduser extends mysqldb
{
var $vusername;
var $vpassword;
function setvariables() {
$this->vusername = $_SESSION['pkmnt_user'];
$this->vpassword = $_SESSION['pkmnt_pass'];
}
function outputtest() {
echo 'Validate User Values: '.$this->vusername.$this->vpassword.'<br>';
echo 'Session Values: '.$_SESSION['pkmnt_user'].$_SESSION['pkmnt_pass'];
}
function checkvalid() {
$sel = "SELECT `id` FROM `members` WHERE username='$this->vusername' AND password='$this->vpassword' LIMIT 1";
$sel = $this->query($sel);
$num = $this->num($sel);
if ($num != 1) {
return false;
}
else {
return true;
}
}
}
?>Im getting mysql errors because the link-id is invalid, because when it was extended, it didnt extend the variables, including the connection variable, so everything crumbles to pieces... I dont know what the solution is. Ive searched for a solution for the past 8 hours, literaly, plus yesterday, there is not one google result that had any code I could look at to see how I could get this to work, and I was really suprised that php class tutorials brought up very very very few actuall tutorials, and none of them did this.
Heres how theyre tied into the script:
Code: Select all
// Includes
require_once "config.php";
require_once "classes/mysql.php";
require_once "classes/validateuser.php";
$db = new mysqldb;
$db->dbhost = $dbhost;
$db->dbusername = $dbusername;
$db->dbpassword = $dbpassword;
$db->dbname = $dbname;
if(!$db->connect()) {
echo 'Error Connecting To The Database.';
exit;
}
// Check if already logged in
$validate = new validateduser;
$validate->setvariables();feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]