Jump to content


Photo

Help with Class


  • Please log in to reply
1 reply to this topic

#1 liboration77

liboration77
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 October 2006 - 02:13 AM

Hi, I am trying to create a login system using a mysql clas that I created mysefl.

However, I get this error:

Fatal error: Call to a member function on a non-object



Here is the beginning of the login script: (login.php)

<?php
$root = './';
$fileRoot = '../';

DEFINE('IN_TCMS','True');



require_once($fileRoot.'sources/classes/class_mysql.php');
$db = New mysql(); 

function main()
{
	session_start();
	if(!isset($_SESSION['username'], $_SESSION['password'])) {
		header("location: login.php?act=01");
	}
	else {
		header("location: index.php");
	}
}

$choice = $_GET['act'];

switch($choice)
{
	case "01":
	do_login();
	break;

	case "02":
	do_logout();
	break;

	default:
	main();
}

function do_login(){

	if (!isset($_POST['valid'])) {
		include($root.'login.html');
	}
	else {

		$username = $_POST['user'];
		$pass = $_POST['pass'];
		$password = SHA1($pass);
		$safeuser = preg_replace("/[^a-z\d]/i",'',$username);
		$db->fetchrow("select * from `users` where `username` = '$safeuser' and `password`='$password'");
		$auth = false;

		if($row['username'] == $safeuser){
			$auth = true;
			session_start();
			$_SESSION['username'] = $safeuser;
			$_SESSION['password'] = $password;

		}
	}

	if($auth){
		session_start();
		echo('Logged in!');
	}
	else{
	}

}

Here is the mysql class: (class_mysql.php)

<?php
$root = '../';

if(!defined("IN_TCMS")) {
	die("Direct access to this file has been denied.");
}

class mysql {


	function query($query) {

		require_( $root . 'config.php' );
		$connection = mysql_connect($INFO['dbhost'], $INFO['dbuser'], $INFO['dbpass']);
		$select = $mysql_select_db($INFO['dbhost'], $connection);

		//Run the query
		$state = mysql_query($query);

		if(!$state) {
			return false;
		}
		mysql_close($connection);
	}


	function fetchrow($query) {

		require_( $root . 'config.php' );
		$connection = mysql_connect($INFO['dbhost'], $INFO['dbuser'], $INFO['dbpass']);
		$select = $mysql_select_db($INFO['dbhost'], $connection);

		$result = mysql_query($query);

		$row = mysql_fetch_row($result);

		if(!$result) {
			return false;
		}
		mysql_close($connection);
	}
}
?>


The error refers to the login.php file at this line:

$db->fetchrow("select * from `users` where `username` = '$safeuser' and `password`='$password'");



Can anyone offer some help on why this is occuring?

#2 MCP

MCP
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 23 October 2006 - 03:24 AM

Your do_login function doesn't know about $db. To let it know that it needs to reference the $db you created in the global scope, add the line

global $db;

as the first line in your do_login function. Otherwise, it will look in the local scope (variables defined in your function), and try to use it. Since you didn't define $db in your local scope, it doesn't know it's an object, so it throws up.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users