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); } ??? 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(); 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! 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? 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. 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. 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
Archived
This topic is now archived and is closed to further replies.