Hey guys,

I have a problem. I have a connections_lib.php file this will handle all of my db connections, this works AMAZINGLY (power of OOP).

Unfortunately some of my other classes require connection to the databse therfore they must use these methods (is that the right word for OOP I'm new, or function whatever).

How would I go about doing this, I get the following error with this code:

Fatal error: Call to a member function start() on a non-object in C:\xampp\htdocs\project5\includes\members_lib.php on line 14

The scripts are ordered in the order they are shown below.

File: connections_lib.php

<?php

	class db {
		var $con;
		var $user;
		var $db;
		var $pass;
		var $host;
		
		function __construct( $type ) {
			if( $type == "main" ) {
				$details = file_get_contents( $_SERVER['DOCUMENT_ROOT'] . "/includes/main.php" );
				$details = explode( "<?php /*" , $details );
				$details = $details[1];
				$details = explode( "*/ ?>" , $details );
				$details = $details[0];
				$details = explode( "-" , $details );
				
				$this->user = $details[1];
				$this->pass = $details[2];
				$this->host = $details[3];
				$this->db = $details[0];
			}
		}
		
		function start() {
			$this->con = mysql_connect( $this->host , $this->user , $this->pass ) or die( mysql_error() );
			mysql_select_db( $this->db , $this->con ) or die( mysql_error() );
		}
		
		function close() {
			mysql_close( $this->con ) or die( mysql_error() );
		}
	}

?>

File: members_lib.php

<?php

	class member {
		var $username;
		var $admin;
		var $first_name;
		var $last_name;
		var $email;
		var $reg_time;
		var $id;
		
		function __construct( $id ) {
			if( isset( $id ) ) {
				$members->start();
				$query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
				if( @mysql_num_rows( $query ) != 1 ) {
					return "<br /><strong>This user can not be found.</strong><br />";	
				} else {
					$row = mysql_fetch_array( $query );
					$this->username = $row['username'];
					$this->first_name = $row['first_name'];
					$this->last_name = $row['last_name'];
					$this->email = $row['email'];
					$this->reg_time = $row['register_time'];
					$this->id = $id;
				}
				$members->close();
			} else {
				return "<br /><strong>You must submit an ID</strong><br />";
			}
		}
		
		function setup( $id ) {
			if( isset( $id ) ) {
				$members->start();
				$query = mysql_query("SELECT * FROM `members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
				if( @mysql_num_rows( $query ) != 1 ) {
					return "<br /><strong>This user can not be found.</strong><br />";	
				} else {
					$row = mysql_fetch_array( $query );
					$this->username = $row['username'];
					$this->first_name = $row['first_name'];
					$this->last_name = $row['last_name'];
					$this->email = $row['email'];
					$this->reg_time = $row['register_time'];
					$this->id = $id;
				}
				$members->close();
			} else {
				return "<br /><strong>You must submit an ID</strong><br />";
			}
		}
		
		function get_reg_format( $format=false ) {
			if( $format == "DD-MM-YY" ) {
				return date( "d-m-y" , $this->reg_time );
			}
			
			if( $format == "DAY-MONTH-YEAR" ) {
				return date( "l jS F Y" , $this->reg_time );	
			}
		}
		
		function update( $field , $new_value ) {
			$members->start();
			$tables = mysql_query("SHOW TABLES");
			while( $row = mysql_fetch_array( $tables ) ) {
				$columns = mysql_query("SHOW COLUMNS FROM `" . $row[0] . "`");
				while( $row2 = mysql_fetch_array( $columns ) ) {
					if( $field == $row2[0] ) {
						$table .= $row[0];
						$update = mysql_query("UPDATE `" . $table . "` SET `" . $field . "` = '" . mysql_real_escape_string( $new_value ) . "' WHERE `id` = '" . mysql_real_escape_string( $this->id ) . "'");
						$query_done++;
					}
				}
			}
			
			if( $query_done == false ) {
				return false;	
			} else {
				return true;
				$this->setup($this->$id);
			}
			$members->close();
		}
	}

?>

File: admin_lib.php

<?php

	class admin {
		var $level;
		var $color;
		var $level_name;
		var $username;
		
		function __construct( $id ) {
			$admin->start();
			if( cookie_set() ) {
				$query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
				if( @mysql_num_rows( $query ) != 1 ) {
					$add_level = mysql_query("INSERT INTO `admin_members` (id, auth_level) VALUES ('" . mysql_real_escape_string( $id ) . "', '1')");
					$query = mysql_query("SELECT * FROM `admin_members` WHERE `id` = '" . mysql_real_escape_string( $id ) . "'");
				}
				$auth = mysql_fetch_array( $query );
				$query_level_dets = mysql_query("SELECT * FROM `admin_levels` WHERE `id` = '" . mysql_real_escape_string( $auth['auth_level'] ) . "'");
				$auth2 = mysql_fetch_array( $query_level_dets );
				$this->level = $auth['auth_level'];
				$this->color = $auth2['color'];
				$this->level_name = $auth2['level_name'];
				$this->id = $id;
			}
			$admin->close();
		}
		
		function get_formatted_username( $username , $link=false ) {
			if( $link == true ) {
				return "<a href=\"" . $site . "members/view.php?id=" . urlencode( $username ) . "\" style=\"color: " . $this->color . "\">" . $username . "</a>";	
			} else {
				return "<span style=\"color: " . $this->color . "\">" . $username . "</span>";	
			}
		}
		
		function get_formatted_admin() {
			return "<span style=\"color: " . $this->color . "\">" . $this->level_name . "</span>";		
		}
		
		function isStaff() {
			if( $this->level_name == "Staff" ) {
				return true;	
			} else {
				return false;	
			}
		}
		
		function isW3hut() {
			if( $this->level_name == "W3Hut" ) {
				return true;	
			} else {
				return false;	
			}
		}
	}

?>

Recommended Answers

All 6 Replies

You have a few options.

You can send an instance to each class.
Ex.

$db = new db;
$admin = new admin( $db,$id ); //send the db object to the class

Or use the singleton method. Look this one up as it wouldn't be easy to show you an example that you could understand.

$members->start();

This requires that $members is an object, created with:

$members = new <classname>;

Where <classname> is the class you want it to be (probably db).

Thanks for the reply.

Is there a way I can do this without having to input a connection object to the admin function?

I'd rather the admin class connects it's self. I have just though of another alternative that may work, could I extend the db class and then use the functions from db?

You could extend it, but every time you called a class a new connection to the database would be made (which isn't good).

I would use the singleton method.

commented: Great suggestions and good understanding of my lacking description of my issue ;) +1

Okay, so could I create the method start() again but use it under admin as it is it's own method? This way it would only connect if the object is used.

As you know I store my connection details in a file in the format:

<?php /*database-username-password-host*/ ?>

So I could just perform the same operations on that as I did for the db object?

Is it possible to use an objects methods as part of another method inside the same object?

@pritaeas - Thanks for the reply also, I did declare the object inside these documents but got unexpected function so I moved the decleration outside of the file and used it before I included the document.

Thanks for the help so far guys ;).

Okay I used what I just described and this seemed to work seemlesley so I have solved my problem.

I would still like to hear back from you if thats okay to let me know if this will infact only connect if a new object is created?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.