Over a million developers have joined DZone.

PHP Clean String For Mysql

DZone's Guide to

PHP Clean String For Mysql

Free Resource
// Function to clean up a string before using it in a mysql query

function clean_for_mysql($string,$max_length) {        
  $in_string = ltrim($string);       
  $in_string = rtrim($in_string);
  if (round($max_length) < 1) {  
    $max_length = 131072; // 128K
  if (strlen($in_string) > $max_length) {
    $new_string = substr($in_string,0,$max_length);
  $new_string = mysql_real_escape_string($new_string);
  return $new_string;

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}