[英]PHP ORDER BY, DESC, LIMIT
I'm hoping for some help: I am trying to output a mysql data table that users can view and arrange by various select fields. 我希望获得一些帮助:我正在尝试输出一个mysql数据表,用户可以通过各种选择字段查看和安排该表。 I want the data to automatically arrange in date order (newest first) and limited to 10 results per page.
我希望数据以日期顺序自动排列(最新的优先),并且每页限制为10个结果。
I have the following script excerpt (I have truncated it for brevity happy to post more if necessary)... 我有以下脚本摘录(为了简便起见,我将其截短了,如果需要的话可以发布更多)...
<!-- **** THIS CODE CREATES THE FORM ****-->
<form id="form1" name="form1" method="post" action="deals.php">
<!-- **** THIS CODE IS FOR THE "FROM" DATE ****-->
<label for="from">From</label>
<input name="from" type="text" id="from" size="10" value="<?php echo $_REQUEST["from"]; ?>" />
Results will only show offers that have been listed since this date. Leave blank for all offers.
<!-- **** THIS CODE IS FOR THE "TO" DATE ****-->
</p>
<label for="to">To</label>
<input name="to" type="text" id="to" size="10" value="<?php echo $_REQUEST["to"]; ?>"/>
Results will only show offers that run until this date. Leave blank for all offers.
<label>Country</label>
<select name="country">
<option value="">--Any--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY country ORDER BY country";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["country"]."'".($row["country"]==$_REQUEST["country"] ? " selected" : "").">".$row["country"]."</option>";
}
?>
</select>
Only shows offers from the selected country.
// ***************************************** // ******* REPEATED FOR EACH VARIABLE (I SHOULD HAVE SET IT UP AS AN ARRAY REALLY ******************************* // ***************************************** // ***************************************** // ***** **重复使用每个变量(我应该将其设置为数组)************************************ // *****************************************
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'')
{
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= ' ORDER BY from_date DESC LIMIT 0, 10 ".mysql_real_escape_string($_REQUEST["from"])."' AND to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;
}
else if ($_REQUEST["from"]<>'')
{$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= ' ORDER BY from_date DESC LIMIT 0, 10".mysql_real_escape_string($_REQUEST["from"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand;
}
else if ($_REQUEST["to"]<>'')
{$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= ' ORDER BY date_time DESC LIMIT 0, 10".mysql_real_escape_string($_REQUEST["to"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;}
else {$sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY from_date DESC LIMIT 0, 10 ".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;
}
This works perfectly when outputting the initial table, however, once users input any data I get the error 这在输出初始表时非常有效,但是,一旦用户输入任何数据,我就会收到错误消息
"request "Could not execute SQL query" SELECT * FROM newoffers ORDER BY from_date DESC LIMIT 0, 10 AND country='UK'"
So I'm guessing there is a problem with the syntax? 所以我猜语法有问题吗? I have tried re-arranging the final part so it reads:
我试图重新安排最后一部分,因此内容如下:
else {$sql = "SELECT * FROM ".$SETTINGS["data_table"]. .$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now; "ORDER BY from_date DESC LIMIT 0, 10";
}
RESULT: Parse error: syntax error, unexpected '"ORDER BY from_date DESC LIMIT' (T_CONSTANT_ENCAPSED_STRING) in /home/iratebjj/public_html/dealstest.php on line 267
结果:解析错误:语法错误,
'"ORDER BY from_date DESC LIMIT' (T_CONSTANT_ENCAPSED_STRING) in /home/iratebjj/public_html/dealstest.php on line 267
意外的'"ORDER BY from_date DESC LIMIT' (T_CONSTANT_ENCAPSED_STRING) in /home/iratebjj/public_html/dealstest.php on line 267
Please check out http://www.iratebjj.com/dealstest.php to see the script in action. 请查看http://www.iratebjj.com/dealstest.php以查看正在运行的脚本。 If anyone has any suggestions I had really appreciate it!
如果有人有任何建议,我真的很感激!
Thanks! 谢谢!
Hope this post is ok. 希望这篇文章可以。 I think I've followed all the guidelines!
我想我已经遵守了所有指南!
Your query formation is wrong. 您的查询格式错误。 The condition
AND country='UK'
should go in a WHERE
condition and not after ORDER BY
clause 条件
AND country='UK'
应该处于WHERE
条件,而不是在ORDER BY
子句之后
SELECT *
FROM newoffers
WHERE country='UK'
ORDER BY from_date DESC
LIMIT 0, 10;
Here's your mistake 这是你的错
$search_Now; "ORDER BY from_date DESC LIMIT 0, 10";
You should concatenate "ORDER BY from_date DESC LIMIT 0, 10"
after $search_Now
and add a space 您应在
$search_Now
之后连接"ORDER BY from_date DESC LIMIT 0, 10"
$search_Now
并添加一个空格
$search_Now." ORDER BY from_date DESC LIMIT 0, 10"
You also need to add WHERE 1 = 1
condition after $SETTINGS["data_table"]
since the following variables seem to begin with AND
: $search_Seller
, $search_country
, $search_id
, $search_Offer
, $search_Item
, $search_Description
, $search_Brand
, $search_Was
, $search_Now
. 您还需要在
$SETTINGS["data_table"]
之后添加WHERE 1 = 1
条件,因为以下变量似乎以AND
开头: $search_Seller
, $search_country
, $search_id
, $search_Offer
, $search_Item
, $search_Description
, $search_Brand
, $search_Was
, $search_Now
。
The last else
block should be as follows 最后的
else
块应如下
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE 1 = 1 ";
$sql .= $search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now." ORDER BY from_date DESC LIMIT 0, 10";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.