Click here to Skip to main content
16,008,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,

I'm having a problem understanding or making the following scenario work well:

I have a login page which in terms has the following:

database/dbsettings.php - containing the require for the dbconn.php and initialization to the class connection.

methods.php containing all the methods in their respective class:
In this case, class Login with its respective functions

login_header.php is just the HTML page that contains the formatting/styles etc.

Now onSubmit, the classes are set and methods are read and given their correct parameters.

What the problem is, is that the dbsettings.php is being ignored, therefore giving me the error:

PHP
Fatal error
: Call to a member function query() on a non-object in
/home2/quzxwetm/public_html/attendance/methods.php
on line
20


dbsettings.php has the initialisation of the class dbconn in dbconn.php

PHP
<?php
//require the class connection to set the database parameters
require("dbconn.php");

$db = new dbconn();

$db->setdatabasename("quzxwetm_attendance");
?>



Line 20 has the following:
PHP
$resultset = $db->query($query);


and in the dbconn.php, there is the following function:

PHP
function query($query)
    {
        if ($connected == false)
        {
            $this->connect();
        }
        $resultset = mysql_query($query) or die(mysql_error());
        //$this->disconnect();
        return $resultset;
    }


PHP
<?php 
include_once 'database/dbsettings.php';
include_once 'methods.php';
require 'login_header.php';
?>

	
	// Set the function for a successful form submission
	function onSubmit($formValues) 
	{
		$formValues = $formValues->loginFormPage->loginFormSection;

		$username = $formValues->username;
		$password = $formValues->password;
		
		$encrypt = md5($password);//for checking with
		
		$myLogin = new Login();
		$myLogin->LoginAttendance($username, $password);
		
		$userid = $_SESSION["userid"];
		$ip = $_SERVER["REMOTE_ADDR"]; //Sender's IP
		
		if($username == $_SESSION["username"] && $encrypt == $_SESSION["password"]) 
				{
					
					if(!empty($formValues->rememberMe)) 
					{
						/*$response = array('successPageHtml' => $got_password.'<p>Login Successful</p><p>We\'ll keep you logged in on this computer.</p>');
						*/
						$myLogin->LogLoginAttendance($userid,$ip);
						
						$expire=time()+30;	//month logged in							
						return array('redirect' => 'index1.php?exp='.$expire);
					}
					else {
						/*$response = array('successPageHtml' => '<p>Login Successful</p><p>We won\'t keep you logged in on this computer.</p>'.$got_password);
						*/
						$myLogin->LogLoginAttendance($userid,$ip);
						
						$expire=time()+60;		//60sec logged in		
						return array('redirect' => 'index1.php?exp='.$expire);
					}
				}
				else 
				{
					$response = array('failureNoticeHtml' => 'Invalid username or password.', 'failureJs' => "$('#password').val('').focus();");
				}
				
		
		return $response;
	}
	
	// Process any request to the form
	$login->processRequest();

?>



Would should be done in order for the function query in dbconn.php be read correctly? And help?

Thanks & Regards,
Francesco
Posted

1 solution

Oh, I now found what was the problem another user posted it from another site and it worked. What the problem was, was that I had to use:
PHP
global $db;

in each function so that it could be read.

So if I have:

PHP
class Login
{

    function loginuser($username, $password)
    {
        global $db;
        
        $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
		
		$resultset = $db->query($query);
    }

}


I hope this helps someone in the future :)
 
Share this answer
 
Comments
Wendelius 19-Dec-11 14:05pm    
Thanks for sharing the solution. 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900