Author Topic: Newbie PHP problem. cant refer to varaible defined in include file  (Read 569 times)

0 Members and 1 Guest are viewing this topic.

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Newbie PHP problem. cant refer to varaible defined in include file
« on: September 16, 2014, 12:25:24 pm »
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

Code: [Select]
<?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
Code: [Select]
<?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!

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Newbie PHP problem. cant refer to varaible defined in include file
« Reply #1 on: September 16, 2014, 12:40:47 pm »
remove the comments from connect.php, before the echo line
and maybe the @ symbol before $db
also your statement is missing a ' before the third argument of your connection call


I would also personally (for this size of a project) just use
require_once

inside your function.

Thats just me personally though. PHP basically treats includes like copy-pastes, except for one thing! depending on server configs iirc... they dont mess with the scope, so you may need to make er a global. IF it still doesnt work after you fix the possible issues above. I am by no means a PHP expert though, just some guy up way too late again trying to help :)
And PLEASE validate your inputs.
« Last Edit: September 16, 2014, 12:41:19 pm by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: Newbie PHP problem. cant refer to varaible defined in include file
« Reply #2 on: September 16, 2014, 12:58:53 pm »
Tried that all out! Didn't work. Then I tried to pass the $db as a parameter to the constructor after creating a private variable called $db in the class so now the constructor becomes
function __construct(DBO $dbh,$uname,$password,$email="xyz@xyz.com",$fname="abcd",$lname="dqrc"){
//an addiotion statement
$this->db=$dbh; //private $db variable set the DBO passed

}

but now it gives a new error on the line where the $db from connect.php is passed to the constructor

//line 91
user($db,$username,$password);
error:
Catchable fatal error: Argument 1 passed to user::__construct() must be an instance of DBO, instance of mysqli given, called in C:\wamp\www\bboard\classes.php on line 91

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: Newbie PHP problem. cant refer to varaible defined in include file
« Reply #3 on: September 16, 2014, 01:04:38 pm »
Finally debuged :D Lawrd sweet homo cheesuz! !!!
Okay, I'll just post it here what I did so that if anyother newbie like me gets stuck he may have guidance!
 I changed the first parameter from DBO to mysqli
now the constructor header becomes
function __construct(mysqli $dbh,$uname,$password,$email="xyz@xyz.com",$fname="abcd",$lname="dqrc")

and wherever the $db->query was used in function definitions I used $this->db->query();
worked like a charm.! Thanks, mate!

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: Newbie PHP problem. cant refer to varaible defined in include file
« Reply #4 on: September 16, 2014, 06:19:59 pm »
Go read up on PDO and prepare & bindings.