开发者

Updating fIle path in MySQL through PHP does not work

I am storing network share paths in my table in MySQL. but some entries got corrupted, i.e they dont have any backslashes in between. Path is like a big word. I wrote a small script in PHP to update these paths to try and add slashes. It does not give me any error when开发者_Python百科 I run it but the results are not getting updated in the table.

Currently the path in my tables is like this: \fil01logsTestDat1oop_s2010Log and I want to make it \\fil01\logs\Test\Dat1\oop_s\2010\Log

If I echo the last $ch7 the results is what I want but it is not going into the table.

$result = mysql_query("select dbresultsid,ResultDirectory from results where dbresultsid > 48717") or die(mysql_error());  
$i= 0;
// store the record of the "example" table into $row
while($row = mysql_fetch_array( $result )) {

$ch = $row['ResultDirectory'];
$id  = $row['dbresultsid'];


if(strstr($ch, $fsd))
{
//echo 'Yes'.$i.'<br>';
//$i++;
$ch1 = str_replace("fil01","\\fil01\\",$ch);
$ch2 = str_replace("logs","logs\\",$ch1);
$ch3 = str_replace("Test","Test\\",$ch2);
$ch4 = str_replace("Dat1","Dat1\\",$ch3);
$ch5 = str_replace("oop_s","oop_s\\",$ch4);
$ch6 = str_replace("2010","2010\\",$ch5);
$ch7 = str_replace("Log","\\Log",$ch6);
//echo trim($ch7).'<br>';
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("opp") or die(mysql_error());
echo "Updating $id with $ch7 <br>";
$update_again = mysql_query("update results set ResultDirectory = \"$ch7\" where dbresultsid = \"$id\" ") or die(mysql_error());  
echo "Updated record $i <br>";
$i++;
}

}

?>

Any ideas?

Thanks.


The backslash is in most environments an escape character. You've basically two solutions:

  1. Use double backslash \\ to represent a single backslash.
  2. Use forward slashes / instead (works perfectly in all platforms, including Windows and *NIX).

I would prefer solution 2 as it works everywhere and would minimize developer confusion.


I got it resolved. The only setting I had to make was this :

$set = mysql_query("set sql_mode='NO_BACKSLASH_ESCAPES';");

before updating.

Hope this helps others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜