OAFC_Rob Posted October 11, 2011 Share Posted October 11, 2011 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; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/ Share on other sites More sharing options...
AbraCadaver Posted October 11, 2011 Share Posted October 11, 2011 The $database object has been instantiated outside of the function that is attempting to use it. It's not even available as an object var. Try passing it into a constructor. Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278330 Share on other sites More sharing options...
OAFC_Rob Posted October 11, 2011 Author Share Posted October 11, 2011 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; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278334 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2011 Share Posted October 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278341 Share on other sites More sharing options...
OAFC_Rob Posted October 11, 2011 Author Share Posted October 11, 2011 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 { } Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278348 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2011 Share Posted October 11, 2011 You wouldn't instantiate the $database object. You would instantiate a new validateForm and since it extends the mysqlDatabaseConnection class, you will have all of the vars/methods from mysqlDatabaseConnection in your validateForm instance. Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278349 Share on other sites More sharing options...
OAFC_Rob Posted October 11, 2011 Author Share Posted October 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278354 Share on other sites More sharing options...
OAFC_Rob Posted October 11, 2011 Author Share Posted October 11, 2011 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 Thanks for the help!! I'm off to go have some food and watch tv / bed. Quote Link to comment https://forums.phpfreaks.com/topic/248914-call-to-a-member-function-escapevalue-on-a-non-object-help/#findComment-1278355 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.