ORDER BY 子句不适用于变量
我有以下代码:
$Ordering=$_POST["fname"];
$CustName=$_POST["CustName"];
echo "Ordering----HHH".$Ordering."HHH".'</br>';
$query="SELECT * FROM Chargebacks WHERE CustName= '$CustName' ORDER BY '$Ordering'";
fname
和 CustName
来自另一个页面。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过删除语句中
$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.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
我很确定您只需要正确连接这些变量即可。
另外,不要使用单引号,因为这会使 SQL 将其视为字符串。使用撇号 (`) 或不使用任何内容 :)
Shai。
I'm pretty sure you just need to concatenate these variables correctly.
Also , don't use single quotes, since thats making SQL treat it as a string. Use either apostrophe (`) or nothing :)
Shai.
不必引用列名称(
$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