开发者

Make Pdo Preserve Backslash

开发者 https://www.devze.com 2023-02-16 03:42 出处:网络
Basically i want to store a path to an image into the database Example:I want to save \'C:\\wampp\\www\\project\\images\' into database

Basically i want to store a path to an image into the database

Example:I want to save 'C:\wampp\www\project\images' into database

Instead Pdo remove all the backslash and make it into 'C:wamppwwwprojectimages'

Is there a way to make Pdo keep the back开发者_StackOverflowslash?

Update with code:

$sql  = "INSERT INTO meal (pic_path) VALUES('C:\wamp\www\project')";
$db   = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute();


The reason that this is happening is because you're using double quotes (") around your query, and since the back slash (\) is the escape character, it's being dropped. You can fix this by placing two back slashes together so it will yield one (C:\\wamp\\www\\project).

However, Passing it to prepare as an argument would be a better idea, and you can keep the double quotes.

$directory = "C:\wamp\www\project";

$sql  = "INSERT INTO meal (pic_path) VALUES(?)";
$db   = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute(array($directory));

Read more about prepared statements.


You should use PDO's variable substitution to safely insert values into the database. To edit your code, it would look like this:

$sql  = "INSERT INTO meal (pic_path) VALUES(?)";
$db   = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute(array('C:\wamp\www\project'));

PDO will replace the ? in your query with the escaped version of the string you pass.

0

精彩评论

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