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;
}
}
}
?>