gpt4 book ai didi

PHP ORDER BY、DESC、LIMIT

转载 作者:行者123 更新时间:2023-11-30 22:54:45 26 4
gpt4 key购买 nike

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

谢谢!

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

最佳答案

这是你的错误

$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 >、$search_De‌ scription$search_Brand$search_Was$search_Now

最后的else block 应该如下所示

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

关于PHP ORDER BY、DESC、LIMIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com