Jump to content

Using my simple database object in another class, object scope not working!!!


Recommended Posts

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]

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 */);
   }
}

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\');');


	}

}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.