Jump to content

Call to a member function escapeValue() on a non-object HELP


OAFC_Rob

Recommended Posts

Hi,

 

i'm having a problem with some coding ive done not too sure why I am getting the following error message Fatal error: Call to a member function escapeValue() on a non-object /commonResources/php.lib/validateForm.class.php on line 82

 

At first I thought it was my database class object wasn't being created, but double checked and I have create a new object called $database of the database class.

 

Any ideas??

 

<?php
require_once ("/home/innova11/public_html/commonResources/dbConnection/dbConnection.php");	
    class validateForm 
{
               public $email, $fName, $surname, $addressLine1, $townCity, $county, $postcode, $contactNum, $comments, $recaptchaVal;
	public $resp;
	public $error;
                function validateRecaptcha2($recaptchaVal)
	{   
                        $this->recaptchaVal = $recaptchaVal;
		if(empty($this->recaptchaVal))
                        {
                           return false;
                        }
                        else
                        {

                            $safe = $database->escapeValue($this->recaptchaVal);
                            $sql = "
                                    SELECT 
                                        *
                                    FROM 
                                        test
                                    WHERE 
                                        test = '".$safe."'
                                  ";
                            $results = $database->sqlQuery($sql);
                            if(mysql_num_rows($results)>0)
                            {
                                
                                return true; 
                            }
                            else
                            {
                                return false;
                            }
                        }
                  }

?>

Link to comment
Share on other sites

Waht do you mean something like this??

 

<?php
require_once ("/home/innova11/public_html/commonResources/dbConnection/dbConnection.php");	
    class validateForm 
{
               public $email, $fName, $surname, $addressLine1, $townCity, $county, $postcode, $contactNum, $comments, $recaptchaVal;
	public $resp;
	public $error;

  function __construct()
{
$database = new mysqlDatabaseConnection();
}
                function validateRecaptcha2($recaptchaVal)
	{   
                        $this->recaptchaVal = $recaptchaVal;
		if(empty($this->recaptchaVal))
                        {
                           return false;
                        }
                        else
                        {

                            $safe = $database->escapeValue($this->recaptchaVal);
                            $sql = "
                                    SELECT 
                                        *
                                    FROM 
                                        test
                                    WHERE 
                                        test = '".$safe."'
                                  ";
                            $results = $database->sqlQuery($sql);
                            if(mysql_num_rows($results)>0)
                            {
                                
                                return true; 
                            }
                            else
                            {
                                return false;
                            }
                        }
                  }

?>

Link to comment
Share on other sites

Yes, or if it is already instantiated in dbConnection.php then pass it into the constructor:

 

$database = new mysqlDatabaseConnection();

$something = new validateForm($database);

 

But on second thought, why not have the validateForm class extend the mysqlDatabaseConnection class?

 

Somebody else may chime in, I have a head cold and my brain is a little fuzzy.

Link to comment
Share on other sites

here is the db connection class coding,

 

<?php
    require_once 'config/config.php';
    
    class mysqlDatabaseConnection 
    {     
        private $dbConnection;
        private $magic_quotes_active;
        private $real_escape_string_exists;
        
        function __construct()
        {
            $this->openDatabaseConnection();
            $this->magic_quotes_active = get_magic_quotes_gpc();
            $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
        }
        
        public function openDatabaseConnection()
        {
            $this->dbConnection = mysql_pconnect(dbHostname,dbUsername,dbPassword);
            if(!$this->dbConnection)
            {
                trigger_error("Error in database connection", E_USER_ERROR);
            }
            else
            {
               $dbSelect = mysql_select_db(dbName,$this->dbConnection); 
               if(!$dbSelect)
               {
                  trigger_error("Error in database selection", E_USER_ERROR); 
               }
            }
        }
        
        public function sqlQuery($sql)
        {
            #NEED TO ADD EXTRA FAIL SAFES IF TABLE DOESN'T EXIST
            
            $result = mysql_query($sql, $this->dbConnection) OR trigger_error(mysql_error($sql),E_USER_ERROR); 
            $this->confirmResult($result);
            return $result;
            #if(mysql_num_rows($result)>0)
            #{
            #  $this->confirmResult($result);
            #  return $result;   
            #}
            #else
            #{
            #   $msg = "Sorry but there seems to be an no data for this query<br/><br/>". $sql;
            #   return $msg;
            #}
            
        }
        
        private function confirmResult($result) 
        {
            if (!$result) 
            {
                $output = "Database query failed: " . mysql_error();
                //$output .= "Last SQL query: " . $this->last_query;
                die( $output );
            }
}
        
        public function escapeValue($value)
        {
            $magic_quotes_active = get_magic_quotes_gpc();
            $new_enough_php = function_exists("mysql_real_escape_string"); //i.e. PHP >= v4.3.0
            if($new_enough_php)
            {
                //PHP v4.3.0 or higher, undo any magic quote effects so mysql_real_espace_string can do the work
                if($magic_quotes_active)
                {
                    $value = stripslashes($value);
                }
                $value = mysql_real_escape_string($value);
            }
            else
            {
                //before PHP v4.3.0, if magic quotes aren't already on the add slashes manually
                if(!$magic_quotes_active)
                {
                    $value = addslashes($value);
                }
                //if magic quotes are active, then the slashes already exist
            }
            return $value; 
        }
        
        #"database-netural methods"
        public function fetchArray($result)
        {
          return mysql_fetch_array($result);  
        }
        
        public function numRows($result)
        {
            return mysql_num_rows($result);
        }

        public function insertId()
        {
            #GETS THE LAST ID INSERTED OVER THE CURRENT DATABASE CONNECTION
            return mysql_insert_id($this->dbConnection);
        }

        public function affectedRows()
        {
            return mysql_affected_rows($this->dbConnection);
        }
        
        public function closeDatabaseConnection()
        {
            if(isset($this->dbConnection))
            {
                mysql_close($this->dbConnection);
                unset ($this->dbConnection);
            }
        }
        
    }
    
    #create a new object of the class 
    $database = new mysqlDatabaseConnection();
    #close the connection
    #$db->closeDatabaseConnection();
?>

 

I just had the same brain wave about extending the class but its seem ages since ive done it, it would start off like this correct? But how would I then pass mysqlDatabaseConnection object ie $database through to it??

 

class validateForm extends mysqlDatabaseConnection
{

}

Link to comment
Share on other sites

Oh, the only issue with that is already have setup through my site $database what have ya's to connect to the dbconnectionclass, how would I get around this? Woudl it be a a matter of setting $database as a new object of the new extended class or have it at the bottom of the new extended class

Link to comment
Share on other sites

Doesn't matter I'm being stupid, I can keep the same object within the dbconnection class and then extend the class but change the $database->escapeValue() for example to $this->escapeValue(). Can you tell it is 19.51 here in the UK and I have been coding since 9am and nto had my tea yet  :P

 

Thanks for the help!!

 

I'm off to go have some food and watch tv / bed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.