Array_map () in PHP calls mysql_real_escape_string clean array

Source: Internet
Author: User
Tags character set php and mysql web database

Use Array_map () to call mysql_real_escape_string to clean up the array

Because mysql_real_escape_string requires a MySQL database connection, you must connect to the MySQL database before calling mysql_real_escape_string.

The code is as follows Copy Code

<?php

Description: Call mysql_real_escape_string to clean the array with Array_map ()

Finishing: Http://www.111cn.net

function Mysqlclean ($data)

{

Return (Is_array ($data)) array_map (' Mysqlclean ', $data): Mysql_real_escape_string ($data);

}

?>

Call method

The code is as follows Copy Code


<?php

$conn = mysql_connect (' localhost ', ' user ', ' pass ');
$_post = Mysqlclean ($_post);

?>


Clean data can be inserted directly into the database.

Attention! Mysql_real_escape_string must be available in the case of (PHP 4 >= 4.3.0, PHP 5). Otherwise you can only use mysql_escape_string, the difference is:

Mysql_real_escape_string takes into account the current character set of the connection, and Mysql_escape_string does not consider it.

Use mysql_real_escape_string to clean and limit character lengths

Because mysql_real_escape_string requires a MySQL database connection, you must connect to the MySQL database before calling mysql_real_escape_string.

When we know that the data type is a string, we can limit the length of the string while cleaning the data. This method comes from David Lane, Hugh E. Williams Web Database application with PHP and MySQL (O ' Reilly, May 2004)

The code is as follows Copy Code

<?php

Description: Use mysql_real_escape_string to clean and limit character length

Finishing: Http://www.111cn.net

function Mysqlclean ($array, $index, $maxlength)

{

if (Isset ($array [$index]))

{

$input = substr ($array ["{$index}"], 0, $maxlength);

$input = mysql_real_escape_string ($input);

return ($input);

}

return NULL;

}

?>

Call Method:

The code is as follows Copy Code

<?php

$conn = mysql_connect (' localhost ', ' user ', ' pass ');

if (isset ($_post[' username '))

{

$_post[' username '] = Mysqlclean ($_post, ' username ', 20);

echo $_post[' username '];

}

?>

Clean the ' username ' in the $_post array and intercept the first 20-bit characters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.