【问题标题】:PHP ORDER BY, DESC, LIMITPHP ORDER BY, DESC, LIMIT
【发布时间】:2014-11-13 09:16:33
【问题描述】:

我希望得到一些帮助: 我正在尝试输出一个用户可以通过各种选择字段查看和排列的 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 以查看正在运行的脚本。 如果有人有任何建议,我真的很感激!

谢谢!

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

【问题讨论】:

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


    【解决方案1】:

    这是你的错误

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

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

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

    您还需要在$SETTINGS["data_table"] 之后添加WHERE 1 = 1 条件,因为以下变量似乎以AND 开头:$search_Seller$search_country$search_id$search_Offer$search_Item、@987654333 @、$search_Brand$search_Was$search_Now

    最后一个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";
    }
    

    【讨论】:

    • 谢谢。我已将原始块替换为: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";但仍然收到错误消息:request "Could not execute SQL query" SELECT * FROM newoffers AND country='UK' ORDER BY from_date DESC LIMIT 0, 10 >>>>>>>>> 我做错了什么吗?感谢您的帮助
    • 查看更新后的答案,我想这就是您要找的。​​span>
    【解决方案2】:

    您的查询格式错误。条件AND country='UK' 应该在WHERE 条件中,而不是在ORDER BY 子句之后

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

    【讨论】:

    • 谢谢,但有几个用户输入字段(其中一个是“国家”,这是“英国”部分的来源)。有没有办法可以将所有这些字段添加到您建议的代码中?
    • @Geoff,是的,你可以。只需在WHERE 子句中包含所有条件。例如,如果您有多个国家/地区用户输入,那么您可以使用 IN 运算符,例如 WHERE country in ('UK','US','IN') (OR) 用于多个单独的字段,使用 AND/OR 条件来检测条件,例如 WHERE country='UK' and city = 'london' or zip = '0099887766'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2014-11-30
    相关资源
    最近更新 更多