snowman15 Posted June 29, 2010 Share Posted June 29, 2010 Hello. The problem I am having is using my custom database class in my membership class which creates memberships with a mysql query. There must be something wrong with the scope or something because whenever i create the new database object with $this->, mysql denies my connection and says I'm not using a password? And sometimes it tells me that my $db->query() is calling a member function on a non-object. I can't figure out how to use my database object to run the createNewMember function and query() the mysql query. I am PRETTY sure my database class works when it is just being called from test.php by itself, not 100%. membership.class.php: <?php include "database.class.php"; class membership{ function __construct(){ $db= new database(); //OBJECT SCOPE NOT BEING FOUND } function createNewMember($username,$password,$password2,$email,$month,$day,$year,$location,$setup){ //BAD DATA CHECKS if ($password != $password2){return "The passwords do not match!";} if (!$this->check_email_address($email)){ return "The email address given is invalid!";} //END BAD DATA CHECKS $emailcode=123; $db->query('INSERT INTO `hookahreview`.`users` (`id`, `username`, `password`, `email`, `location`, `birthday`, `num_reviews`, `setup`, `date_added`, `email_code`, `active`) VALUES (NULL, \''.$username.'\', \''.md5($password).'\', \''.$email.'\', \''.$location .'\', \''.$year.'-'.$month.'-'.$day.'\', \'0\', \''.$setup.'\', CURDATE() ,\''.$emailcode.'\', \'0\');'); // THIS QUERY SAYS IT BEING CALLED ON NON-OBJECT } } ?> database.class.php <?php class database{ function connect(){ $hostname='localhost'; $username='root'; $password='######'; $dbname='hookahreview'; connection=mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname,connection); if ($connection){ return 1;}else {return 0;} } function query($query){ $sql = "".$query.";"; $result = mysql_query($sql); return $result; } } ?> My test.php !! <?php include "classes/membership.class.php"; $membership= new membership(); $membership->createNewMember($username,$password,$password2,$email,$month,$day,$year,$location,$setup) ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/206177-using-my-simple-database-object-in-another-class-object-scope-not-working/ Share on other sites More sharing options...
KevinM1 Posted June 29, 2010 Share Posted June 29, 2010 You need to use the 'this' keyword: class Membership { private $db; public function __construct() { $this->db = new Database(); } public function createNewMember(/* args */) { // ... $result = $this->db->query(/* query */); } } Quote Link to comment https://forums.phpfreaks.com/topic/206177-using-my-simple-database-object-in-another-class-object-scope-not-working/#findComment-1078710 Share on other sites More sharing options...
snowman15 Posted June 29, 2010 Author Share Posted June 29, 2010 Ok i changed it and I get this error: Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/11/6328911/html/test/classes/database.class.php on line 21 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/11/6328911/html/test/classes/database.class.php on line 21 Here is my new code: <?php class database{ function connect(){ $hostname='hookahreview.db.6328911.hostedresource.com'; $username='hookahreview'; $password='Hellodolly123'; $dbname='hookahreview'; $connection=mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname,connection); if ($connection){ return 1;}else {return 0;} } function query($query){ $sql = "".$query.";"; $result = mysql_query($sql); return $result; } } ?> <?php include "database.class.php"; class membership{ private $db; function __construct(){ $this->db= new database(); } function createNewMember($username,$password,$password2,$email,$month,$day,$year,$location,$setup){ //BAD DATA CHECKS if ($password != $password2){return "The passwords do not match!";} if (!$this->check_email_address($email)){ return "The email address given is invalid!";} //END BAD DATA CHECKS $emailcode=123; $result = $this->db->query('INSERT INTO `hookahreview`.`users` (`id`, `username`, `password`, `email`, `location`, `birthday`, `num_reviews`, `setup`, `date_added`, `email_code`, `active`) VALUES (NULL, \''.$username.'\', \''.md5($password).'\', \''.$email.'\', \''.$location .'\', \''.$year.'-'.$month.'-'.$day.'\', \'0\', \''.$setup.'\', CURDATE() ,\''.$emailcode.'\', \'0\');'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206177-using-my-simple-database-object-in-another-class-object-scope-not-working/#findComment-1078718 Share on other sites More sharing options...
KevinM1 Posted June 29, 2010 Share Posted June 29, 2010 Can you connect not using the socket? Also, you may want to test your db code before mashing it into another class. Quote Link to comment https://forums.phpfreaks.com/topic/206177-using-my-simple-database-object-in-another-class-object-scope-not-working/#findComment-1078763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.