2levelsabove Posted March 24, 2010 Share Posted March 24, 2010 How can I make each member data of an object mysql_real_escape_string ? Im drawing a blank due to lack of sleep. Thank you foreach ($obj as &$value) { $value = mysql_real_escape_string($value); } ??? Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/ Share on other sites More sharing options...
KevinM1 Posted March 24, 2010 Share Posted March 24, 2010 Try: foreach ($this as $value) { $value = mysql_real_escape_string($value); } Keep in mind, this will apply mysql_real_escape_string to every data member of your object. This also assumes you're using this code from within the object itself, like so: class test { private $firstName = "Forrest"; private $lastName = "Gump"; public function escapeData() { foreach ($this as $value) { $value = mysql_real_escape_string($value); } } } $example = new test(); $example->escapeData(); Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/#findComment-1031220 Share on other sites More sharing options...
2levelsabove Posted March 24, 2010 Author Share Posted March 24, 2010 Thanks a lot for the reply. I was actually type casting a POST array $input = (object)($_POST); and then wanted to clean up the input. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/#findComment-1031221 Share on other sites More sharing options...
KevinM1 Posted March 24, 2010 Share Posted March 24, 2010 Why would you want to cast $_POST as an object? Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/#findComment-1031223 Share on other sites More sharing options...
ignace Posted March 24, 2010 Share Posted March 24, 2010 You can also just do: $_POST = array_map('mysql_real_escape_string', $_POST); Which will apply mysql_real_escape_string to each value. Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/#findComment-1031239 Share on other sites More sharing options...
2levelsabove Posted March 24, 2010 Author Share Posted March 24, 2010 I know this will sound retarded but I did it because i like the object syntax As opposed to writing : $sql="INSERT INTO DB VALUES(".$_POST['test'].")"; I can do $sql="INSERT INTO DB VALUES("$obj->test")"; I know this is sad. Quote Link to comment https://forums.phpfreaks.com/topic/196402-objects-mysql_real_escape_string/#findComment-1031248 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.