试图在别处找到答案,但没有成功。任何想要纠正我的人,我确信我遗漏了一些基本的东西 - 我找不到什么。
我有11个列的表'数据',inc'from_Date'和to_date'。目前正在显示的结果是日期最远的未来。我要求结果首先显示为最接近的日期(即ASC/DESC
)。
尝试ORDER BY
from_date - 没有效果。样本数据/页面'结果在my search表格代码(参考)
<form action="webdeals.php" method="post" name="form1" id="form1" style="margin-bottom: 0;">
<table width="839" border="0">
<tr valign="top">
<td width="83"> </td>
<td width="105"><span class="style31">Course Type:</span>
</td>
<td width="161">
<select name="city">
<option value="">--</option>
<?php $sql="SELECT * FROM " .$SETTINGS[ "data_table"].
" GROUP BY city ORDER BY city"; $sql_result=m ysql_query ($sql, $connection ) or die (
'request "Could not execute SQL query" '.$sql); while ($row=m ysql_fetch_assoc($sql_result)) { echo
"<option value='".$row[ "city"]. "'
".($row[ "city"]==$_REQUEST[ "city"] ? "selected" : ""). ">".$row[
"city"]. "</option>"; } ?>
</select>
</td>
<td width="100" class="style27"><span class="style31">From:</span>
</td>
<td width="108">
<input name="from" type="text" id="from" size="10" value="
<?php echo $_REQUEST [" from "]; ?>" />
</td>
<td width="104">
<img src="images/calendar_icon_pack.png" width="32" height="32" />
</td>
<td width="153">
<div align="center">
<input type="image" src="http://www.scotsail.co.uk/images/filter_button.png"
alt="Refine Results" />
</div>
</td>
</tr>
<tr valign="top">
<td class="style23"> </td>
<td class="style31">Price Search:</td>
<td>
<input type="text" name="string" id="string" value="
<?php echo stripcslashes($_REQUEST[" string "]); ?>" />
</td>
<td>
<label for="to2" class="style31">To:</label>
</td>
<td>
<input name="to" type="text" id="to" size="10" value="<?php echo $_REQUEST["
to "]; ?>"/>
</td>
<td>
<img src="images/calendar_icon_pack.png" width="32" height="32" />
</td>
<td>
<div align="center"><a href="webdeals.php">Reset</a>
</div>
</td>
</tr>
</table>
<label for="from" class="style2"></label>
</form>
然后:
<?php
if ($_REQUEST[ "string"]<>'') {
$search_string = " AND (full_name LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR email LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["city"]<>'') {
$search_city = " AND city='".mysql_real_escape_string($_REQUEST["city"])."'";
}
if ($_REQUEST["from"] <>'' and $_REQUEST["to"] <>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date
>= ' ".mysql_real_escape_string($_REQUEST["from"])."' AND to_date
<='".mysql_real_escape_string ($_REQUEST["to"])."' ".$search_string.$search_city;
} else
if ($_REQUEST["from "]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table "]." WHERE from_date>= ' ".mysql_real_escape_string ($_REQUEST["from"])."'".$search_string.$search_city;
} else
if ($_REQUEST["to"] <>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date
<='".mysql_real_escape_string ($_REQUEST["to"])."' ".$search_string.$search_city;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table "]." WHERE id>0".$search_string.$search_city;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
如果这是一个基本问题,请道歉,但是因为无法使它发挥作用而感到遗憾。
更新: 感谢@Veger和其他人的评论。
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0 ORDER BY from_date ".$search_string.$search_city;
}
确实会导致“未定义”结果按from_date排序,但是这会打破“city”列/字段的搜索,并且在调用from_date和to_date时不起作用。关于它应该如何看待的任何想法?
答案 0 :(得分:0)
你在$string_string
语句之后添加了ORDER BY
,这会导致错误的SQL查询:
SELECT * FROM <table>
WHERE id>0
ORDER BY from_date AND (full_name LIKE '%<fullname>%')
你需要交换两者:
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0 ".$search_string.$search_city." ORDER BY from_date";
所以你的查询就像:
SELECT * FROM <table>
WHERE id>0 AND (full_name LIKE '%<fullname>%')
ORDER BY from_date
有关ORDER BY
语法和语句排序的更多信息,请查看mysql ORDER BY
documentation。