使 Pdo 保留反斜杠

发布于 2024-10-21 17:21:12 字数 404 浏览 5 评论 0原文

基本上我想将图像的路径存储到数据库中

示例:我想将 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-10-28 17:21:12

发生这种情况的原因是因为您在查询周围使用双引号 ("),并且由于反斜杠 (\) 是转义字符,因此它被您可以通过将两个反斜杠放在一起来解决此问题,这样它就会产生一个(C:\\wamp\\www\\project),

但是,将其传递给prepare。 > 作为参数会是一个更好的主意,并且您可以保留双引号

$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));

阅读更多内容 关于准备好的陈述

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.

惯饮孤独 2024-10-28 17:21:12

您应该使用 PDO 的变量替换来安全地将值插入数据库。要编辑代码,它看起来像这样:

$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 将替换 ?在您的查询中使用您传递的字符串的转义版本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文