ORDER BY 子句不适用于变量

发布于 2024-12-12 00:51:07 字数 702 浏览 0 评论 0原文

我有以下代码:

$Ordering=$_POST["fname"];
$CustName=$_POST["CustName"];

echo "Ordering----HHH".$Ordering."HHH".'</br>';

$query="SELECT * FROM Chargebacks WHERE CustName= '$CustName' ORDER BY '$Ordering'";

fnameCustName 来自另一个页面。 fname 工作正常,CustName 正确地回显为 $Ordering (“HHH”在那里,所以我可以确定没有偷偷摸摸的空格。 ...)

ORDER BY 子句根本不执行任何操作...如果输入无效的列标题,则脚本会按预期崩溃,但如果使用有效的列标题,则结果的顺序很简单它们在源表中出现的顺序。
如果我复制并粘贴回显的 $Ordering 值并将其放入代码中(不带单引号),则 ORDER BY 子句可以正常工作。

在研究中,我发现了单引号和双引号以及普通括号、方括号和大括号主题的许多变体,但它们都不起作用,我确信这样的事情应该没有必要。
就是这么简单的事情......不是吗?

I have the following code:

$Ordering=$_POST["fname"];
$CustName=$_POST["CustName"];

echo "Ordering----HHH".$Ordering."HHH".'</br>';

$query="SELECT * FROM Chargebacks WHERE CustName= '$CustName' ORDER BY '$Ordering'";

Both fname and CustName come from another page. fname works fine, CustName is echoed correctly as $Ordering ( the "HHH" is there so I could be sure there were not sneaky spaces ....)

The ORDER BY clause simply does nothing...if an invalid column heading is entered, the sript simply crashes as expected, but if a valid one is used, the order of results is simply the order they appear in the source table.
If I copy and paste the value of the echoed $Ordering and put it in the code (without the single quotes) then the ORDER BY clause works fine.

In research, I have found any number of variations on a theme of single and double quotes, and normal, square and brace bracketes, but none of them has worked and I am sure such things should not be necessary.

It is such simple thing...isn't it ?

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

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

发布评论

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

评论(4

假情假意假温柔 2024-12-19 00:51:07

您是否尝试过删除语句中 $Ordering 周围的引号?您自己说过,如果删除引号并将其替换为变量的内容,它就会起作用。

Have you tried removing the quotes from around $Ordering within your statement? You said yourself that if you remove the quotes and replace it with the content of the variable it works.

枕梦 2024-12-19 00:51:07

1) $Ordering 必须是 Chargebacks 表中的表属性
2)删除$Ordering两边的单引号

1) $Ordering must be a table attribute from the table Chargebacks
2) remove the single quotes from both side of $Ordering

等风来 2024-12-19 00:51:07

我很确定您只需要正确连接这些变量即可。

$query="SELECT * FROM Chargebacks WHERE CustName='".$CustName."' ORDER BY `".$Ordering."`";

另外,不要使用单引号,因为这会使 SQL 将其视为字符串。使用撇号 (`) 或不使用任何内容 :)

Shai。

I'm pretty sure you just need to concatenate these variables correctly.

$query="SELECT * FROM Chargebacks WHERE CustName='".$CustName."' ORDER BY `".$Ordering."`";

Also , don't use single quotes, since thats making SQL treat it as a string. Use either apostrophe (`) or nothing :)

Shai.

往日 2024-12-19 00:51:07

不必引用列名称($Ordering 表示字段名称)。
如果必须引用标识符,请使用 `.

对于引用: http://dev.mysql.com/doc/refman /5.0/en/identifiers.html

Quoting of a column name ($Ordering representing a field name) should not be necessary.
If you must quote the identifier, use `.

For the quoting: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

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