liboration77 Posted October 23, 2006 Share Posted October 23, 2006 Hi, I am trying to create a login system using a mysql clas that I created mysefl.However, I get this error:[quote][b]Fatal error:[/b] Call to a member function on a non-object[/quote][b]Here is the beginning of the login script: (login.php)[/b][code]<?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{ }}[/code][b]Here is the mysql class: (class_mysql.php)[/b][code]<?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); }}?>[/code]The error refers to the login.php file at this line:[quote] $db->fetchrow("select * from `users` where `username` = '$safeuser' and `password`='$password'");[/quote]Can anyone offer some help on why this is occuring? Quote Link to comment Share on other sites More sharing options...
MCP Posted October 23, 2006 Share Posted October 23, 2006 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[code=php:0]global $db;[/code]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. Quote Link to comment 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.