Home » Solved Issues

script to remove extra white spaces from each and every fields of whole database table

20 July 2010 One Comment

Posts and pages
here is simple script to remove extra white spaces from each field of database table and get log record of how many fields age getting affected ! !

<?php
mysql_connect("localhost","root","");
mysql_select_db("temp");
if(isset($_GET['table']))
{
define("TABLE_NAME",$_GET['table']);
$sql = "select * from ".TABLE_NAME." ";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
$flag = 0;
$i = 0;
foreach($row as $key => $value) {
if($flag == 0) {
$flag = 1;
} else {
$flag = 0;
$trimed = trim($value);
$temp = "update ".TABLE_NAME." set ".$key." = '".$trimed."' where id = '".$row[0]."' ";
mysql_query($temp);
if(mysql_affected_rows() == "1")
{
$i++;
echo "field trimed at id = ".$row[0]." and field = ".$key."";
}
} } }
if($i == "0") {
echo "no rows affected";
}
echo "DATABASE table trimed successfully";
}
else {
echo "no table defined";
}
?>

One Comment »

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.