note 92159 added to function.mysql-real-escape-string
| From: | isaacs dot brian dot c at gmail dot com | Date: | Sun, 12 Jul 2009 06:06:28 +0000 |
| Subject: | note 92159 added to function.mysql-real-escape-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158193@lists.php.net to get a copy of this message | ||
I had wanted to see the merits of dynamically and "automatically" applying
mysql_real_escape_string() to $_GET and $_POST as arrays rather than manually each time I would type
up a query. I used array_walk_recursive() to call my function "mysql_safe" to apply
mysql_real_escape_string() to each key of the $_GET and $_POST arrays.
My function is part of a class, and it is called each time I connect to the database to perform a
query:
<?php
class MyClass {
function mysql_safe ( $item, $key, $type )
{
switch( $type )
{
case 'get':
if( count( $_GET ) > 0 )
{
$_GET[$item] = mysql_real_escape_string( $key );
}
break;
case 'post':
if( count( $_POST ) > 0 )
{
$_POST[$item] = mysql_real_escape_string( $key );
}
break;
}
}
function safe_get ( )
{
#Flag to only run function once
if( $this->get_flag == true ) { return true; }
array_walk_recursive( $_GET, array( $this, 'mysql_safe' ), 'get' );
array_walk_recursive( $_POST, array( $this, 'mysql_safe' ), 'post' );
$this->get_flag = true;
}
}
?>
However, after using this function, I find that it does indeed work, it also creates new $_GET and
$_POST values in which $item and $key are swapped. So I end up with $_GET[$item] = $key, as well as
$_GET[$key] = $item. I have not yet determined if this is due to the actual coding itself, or my
particular method of implementation.
----
Server IP: 64.71.164.2
Probable Submitter: 98.206.177.34
----
Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php
Edit -- https://master.php.net/note/edit/92159
Del: integrated -- https://master.php.net/note/delete/92159/integrated
Del: useless -- https://master.php.net/note/delete/92159/useless
Del: bad code -- https://master.php.net/note/delete/92159/bad+code
Del: spam -- https://master.php.net/note/delete/92159/spam
Del: non-english -- https://master.php.net/note/delete/92159/non-english
Del: in docs -- https://master.php.net/note/delete/92159/in+docs
Del: other reasons-- https://master.php.net/note/delete/92159
Reject -- https://master.php.net/note/reject/92159
Search -- https://master.php.net/manage/user-notes.php