替代 '或“?

发布于 2024-10-31 05:27:12 字数 398 浏览 2 评论 0 原文

我正在使用 PHP fwrite 将 HTML 和 PHP 输入到文件中 问题是这样的,我使用了 ' ' 来包围 $content 是什么,即。 HTML 和 PHP,

括起来

 $sql="SELECT * FROM $tbl_name WHERE..... 

然后我使用“”将$content 中的内容

,但现在当我想这样做时我陷入困境:

  WHERE var1="$var1" and var2="$var2"  

因为我已经使用了“”和“”,如果我使用其中之一,显然它行不通

有什么想法可以用替代方法来解决这个问题吗?还有其他类似于 ' 或 " 的符号吗?

提前致谢 尼尔

I am using PHP fwrite to enter HTML and PHP into a file
The problem is this, I have used ' ' to enclose what $content is ie. the HTML and PHP,

Then I used " " to enclose

 $sql="SELECT * FROM $tbl_name WHERE..... 

which is in $content,

but now i am stuck when i want to do this:

  WHERE var1="$var1" and var2="$var2"  

because I've already used ' ' and " " and if i used one of these it obiviously wouldn't work

Any ideas for an alternative way to get around this problem?? is there any other symbols similar to ' or "?

Thanks in advance
Niall

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

心欲静而疯不止 2024-11-07 05:27:12
$sql = 'SELECT * FROM ' . $tbl_name . ' WHERE var1="' . $var1 . '"';

话虽这么说,除非 $var1 是整数或以其他方式清理,否则您的代码将容易受到 SQL 注入< /a>.

$sql = 'SELECT * FROM ' . $tbl_name . ' WHERE var1="' . $var1 . '"';

That being said, unless $var1 is an integer or otherwise sanitized, your code will be vulnerable to SQL injections.

夜无邪 2024-11-07 05:27:12

您可以通过在前面放置反斜杠来转义“或 ' 字符:

$sql = "SELECT * FROM $tbl_name WHERE var1=\"$var1\" and var2=\"$var2\""

如果您不想受到 SQL 注入的攻击,请确保在执行此查询之前对 $var1 和 $var2 调用addslashes(或任何类似的内容)

You can escape the " or ' character by putting backslashes in front of it:

$sql = "SELECT * FROM $tbl_name WHERE var1=\"$var1\" and var2=\"$var2\""

Please make sure you call addslashes (or anything similar) on $var1 and $var2 before executing this query, if you do not want to be vulnerable to SQL Injection.

那一片橙海, 2024-11-07 05:27:12

你可以这样做:

 $sql='select * from '.$tblname.' where...

you could do this:

 $sql='select * from '.$tblname.' where...
巴黎盛开的樱花 2024-11-07 05:27:12

你不需要任何替代品。

只是不要将 PHP 代码与您的代码一起写入文件中。

看,你使用 PHP 的方式非常错误。

您不必为每个新用户编写另一个 PHP 页面,而只需使用一个 PHP 页面即可为所有用户提供服务。

该页面将操作一些数据存储,数据库或文本文件,并将其显示给用户。

这就是每个 PHP 应用程序的工作方式:单个 PHP 代码,多个数据。

You don't need any alternatives.

Just do not write PHP code into a file with your code.

Look, you are using PHP awfully wrong way.

Instead of writing another PHP page for the every new user you have to use only one PHP page to serve them all.

This page would operate some data storage, a database or a text file, and display it to the user.

That's the way every PHP application works: single PHP code, multiple data.

懵少女 2024-11-07 05:27:12

您可以使用反斜杠转义引号。所以在你的 $sql 变量中你可以使用

\" Escaped Double Quote.
\' Escaped Single Quote.

You can escape your quotes with a backslash. So in your $sql variable you can use

\" Escaped Double Quote.
\' Escaped Single Quote.
带上头具痛哭 2024-11-07 05:27:12

您也可以简单地在 SQL 查询中使用单引号:

$sql = "SELECT * FROM $tbl_name WHERE var1='some string value' AND var2='$var2'";

或者只使用字符串连接,这适用于单引号和双引号字符串:

$sql = "SELECT * FROM $tbl_name WHERE var1='" . $var1 . "' AND var2='" . $var2 . "'";
$sql = 'SELECT * FROM ' . $tbl_name . ' WHERE var1="'. $var1 . '" AND var2="' . $var2 . '"';

You can simply use single quote inside of SQL queries as well:

$sql = "SELECT * FROM $tbl_name WHERE var1='some string value' AND var2='$var2'";

Or just use string concatenation, which works for both single and double enquoted strings:

$sql = "SELECT * FROM $tbl_name WHERE var1='" . $var1 . "' AND var2='" . $var2 . "'";
$sql = 'SELECT * FROM ' . $tbl_name . ' WHERE var1="'. $var1 . '" AND var2="' . $var2 . '"';
梨涡 2024-11-07 05:27:12

虽然上面的所有答案都是正确的,但我想指出一个更优雅的解决方案:

sprintf('SELECT * FROM %s WHERE var1 = "%s" AND var2 = %d', "tableName, "someString", 12);

While all the answers above are correct I want to point out a more elegant solution:

sprintf('SELECT * FROM %s WHERE var1 = "%s" AND var2 = %d', "tableName, "someString", 12);
慕巷 2024-11-07 05:27:12

如果您已经将字符串括在“”中,则无需在 $var1 和 $var2 周围再次添加它们

If you have already enclosed the string in "", you do not need to add them again around $var1 and $var2

玩物 2024-11-07 05:27:12

您不需要任何 ' 或 " 的替代品。
您可以通过在其前面放置 \ 来转义 ' 或 ",

例如

$sql = "SELECT * from $table_name where var1=\"$var1\" and var2=\"$var2\""

执行后,$sql 中的字符串将是

SELECT * from my_table where var1="var1_value" and var2="var2_value"

You don't need any alternate to ' or ".
You can escape ' or " by placing \ before it

for example

$sql = "SELECT * from $table_name where var1=\"$var1\" and var2=\"$var2\""

after execution, string inside $sql will be

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