PHP ORDER BY,DESC,LIMIT

时间:2014-11-13 09:16:33

标签: php mysql sql-order-by sql-limit

我希望得到一些帮助: 我正在尝试输出一个mysql数据表,用户可以通过各种选择字段查看和排列。我希望数据按日期顺序自动排列(最新的第一个),每页限制为10个结果。

我有以下脚本摘录(我为了简洁而截断了它,如果有必要,可以发布更多信息)...

<!-- **** 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.

// ***************************************** // *******为每个变量重复(我应该把它设置为阵列真的很棒************************** ***** // *****************************************

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;
}

这在输出初始表时非常有效,但是,一旦用户输入任何数据,我就会得到错误

"request "Could not execute SQL query" SELECT * FROM newoffers ORDER BY from_date DESC LIMIT 0, 10 AND country='UK'"

所以我猜测语法有问题吗? 我已经尝试重新安排最后一部分,所以它写着:

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";
}

结果:解析错误:语法错误,意外'"ORDER BY from_date DESC LIMIT' (T_CONSTANT_ENCAPSED_STRING) in /home/iratebjj/public_html/dealstest.php on line 267

请查看http://www.iratebjj.com/dealstest.php以查看正在运行的脚本。 如果有人有任何建议我真的很感激它!

谢谢!

希望这篇文章没问题。我想我已经遵循了所有准则!

2 个答案:

答案 0 :(得分:0)

您的查询形成错误。条件AND country='UK'应该处于WHERE条件而不是ORDER BY子句之后

SELECT * 
FROM newoffers 
WHERE country='UK'
ORDER BY from_date DESC 
LIMIT 0, 10;

答案 1 :(得分:0)

这是你的错误

$search_Now; "ORDER BY from_date DESC LIMIT 0, 10";

您应该在"ORDER BY from_date DESC LIMIT 0, 10"之后连接$search_Now并添加空格

$search_Now." ORDER BY from_date DESC LIMIT 0, 10"

您还需要在WHERE 1 = 1之后添加$SETTINGS["data_table"]条件,因为以下变量似乎始于AND$search_Seller$search_country,{{1} },$search_id$search_Offer$search_Item$search_De‌​scription$search_Brand$search_Was

最后一个$search_Now块应如下所示

else