使 Pdo 保留反斜杠
基本上我想将图像的路径存储到数据库中
示例:我想将 'C:\wampp\www\project\images'
保存到数据库中
而不是 Pdo 删除所有反斜杠并将其放入'C:wamppwwwprojectimages'
有没有办法让 Pdo 保留反斜杠?
使用代码更新:
$sql = "INSERT INTO meal (pic_path) VALUES('C:\wamp\www\project')";
$db = new PDO( $dsn, $username, $password );
$stmt = $db->prepare( $sql );
$stmt->execute();
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 backslash?
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况的原因是因为您在查询周围使用双引号 (
"
),并且由于反斜杠 (\
) 是转义字符,因此它被您可以通过将两个反斜杠放在一起来解决此问题,这样它就会产生一个(C:\\wamp\\www\\project
),但是,将其传递给
prepare
。 > 作为参数会是一个更好的主意,并且您可以保留双引号阅读更多内容 关于准备好的陈述。
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.Read more about prepared statements.
您应该使用 PDO 的变量替换来安全地将值插入数据库。要编辑代码,它看起来像这样:
PDO 将替换 ?在您的查询中使用您传递的字符串的转义版本。
You should use PDO's variable substitution to safely insert values into the database. To edit your code, it would look like this:
PDO will replace the ? in your query with the escaped version of the string you pass.