I was just trying to create a bulletin board so there will be a user class the constructor of which will check if the username and password is given or all other info including the fname lastname too.
It checks for parameters and runs two private functions authenticate() or registerme() which ever makes sense to it. Now here's the problem I've the connection variables defined in an external file connect.php which im trying to include with require_once.
But the function definition in the class definition can't find the variable $db ($db=mysqli_connect(blabla))
The error is "undefined variable db on line bla bla"
heres the code
<?php
require_once("connect.php");//just once okay?
session_start();
class user{
//define variables
private $fname;
private $lname;
private $uname;
private $password;
private $email;
private $totalposts;
private function authenticate($uname,$password){
$query="SELECT firstname,lastname,email,totalposts from users where username='$uname'&&password='$password'";//query created
$result=$db->query($query);
if(!$result) return false;
else{
$row=$result->mysqli_assoc();//okay row grabbed
$this->uname=$name;
$this->passowrd=$password;
$this->fname=$row['firstname'];
$this->lname=$row['lastname'];
$this->email=$row['email'];
$this->totalposts=$row['totalposts'];
return true;
}//else ends here
}//authenticate function ends here
private function registerme($uname,$password,$email,$fname,$lname){
$query="INSERT INTO users(username,password,email,firstname,lastname) VALUES('$uname','$password','$email','$fname','$lname')";
$db->query($query);
$this->uname=$uname;
$this->password=$password;
$this->email=$email;
$this->fname=$fname;
$this->lname=$lname;//okay object variables set
$this->totalposts=0;
return true;
}//register me ends heres
public function getemail(){return $this->email;}
public function getfname(){return $this->fname;}
function __construct($uname,$password,$email="xyz@xyz.com",$fname="abcd",$lname="dqrc"){
if($email="xyz@xyz.com"&&$fname="abcd"&&$lname="dqrc"){
$logged=user::authenticate($uname,$password);//check if it is authenticated
if($logged){
$_SESSION['uname']=$this->uname;
$_SESSION['totalposts']=$this->totalposts;
$_SESSION['email']=$this->email;
$_SESSION['fname']=$this->fname;
$_SESSION['lname']=$this->lname;
}//log in condition
}//if the user is already registered and enters only username and password
else if($uname!=''&&$password!=''&&$email!=''&&$fname!=''&&$lname!=''){
$registered=registerme();
if($registered){
$_SESSION['uname']=$this->uname;
$_SESSION['totalposts']=$this->totalposts;
$_SESSION['email']=$this->email;
$_SESSION['fname']=$this->fname;
$_SESSION['lname']=$this->lname;
}//if its registereds
}
}//constructor ends
}//class definition ends
$user1=new user('user1','password1');
$username=$user1->getemail();
$firstname=$user1->getfname();
echo "Your username is $username and firstname is $firstname";
?>
*****************************************************************
heres connect.php
<?php
@ $db=new mysqli('localhost','xuz',shuz','bboard');
if(mysqli_connect_errno()){
echo"Cant connect";exit;
}
//else echo 'conneted';//here we have hopefully connected to the database
?>
the line "$result=$db->query($query);" in the definition of authenticate generates the error ! Please help!