-1

I'm very new to PHP. Currently,I'm facing one issue to escape \ and store in mysql DB. eg., If the string is like $str = \\aaaa\bbb\\\cccc$$ , I would like to save that string in DB . However, I don't know how to escape those backslashes. Please help me settle it.

hhkhaing
  • 1
  • 1

1 Answers1

2

Your best option is to use prepared statements (e.g. through PDO):

$dbh = new PDO(…);
$sth = $dbh->prepare('INSERT INTO `yourtable` ( `column` ) VALUES ( :string )');
$sth->execute(array(':string' => '\\aaaa\bbb\\\cccc$$'));
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Just a link to another question that may have some more useful information: [PHP PDO prepared statements](http://stackoverflow.com/questions/1457131/php-pdo-prepared-statements) – Janis Veinbergs Sep 28 '11 at 10:13
  • Thank you.. I've tried that way and '\aaaa\bbb\\cccc$$' is saved to DB rather than '\\aaaa\bbb\\\cccc$$' . – hhkhaing Sep 28 '11 at 12:07
  • Are you sure? Single quotation marks don't interpret escape sequences and prepared statements store values ›as is‹. Maybe you are outputting them incorrectly? – knittl Sep 28 '11 at 12:13
  • Here's my codes $dbo = new PDO("mysql:host=localhost;dbname=test4;","root","123"); $sql = 'INSERT INTO `table1` ( `name` ) VALUES ( :string )'; $sth = $dbo->prepare($sql); $sth->execute(array(':string' => '\\aaaa\bbb\\\cccc$$')); – hhkhaing Sep 28 '11 at 12:15
  • How do you verify what has been inserted into your db? – knittl Sep 28 '11 at 12:52
  • I go and see in mysql(phpadmin). – hhkhaing Sep 28 '11 at 13:54