我想重复我想要的结果,如何进一步过滤它们?
示例,search x100 y100,目前得到了数百个结果。我需要进一步过滤这些结果,这样我才能得到那些标记为敌对、待定或友好的结果。
我有以下html表单
<form action="xysearch.php" method="post">
<label>X Coord
<input type="text" name="x" />
</label>
<label>Y Coord
<input type="text" name="y" />
</label>
<select name="term">
<option value="Hostile">Hostile</option>
<option value="Pending">Pending</option>
<option value="Friendly">Friendly</option>
</select>
<input type="submit" value="Search" />
</form>我需要添加到搜索查询中的是一种过滤这些结果的方法,所以只显示外交下拉列表中选择的选项。
到目前为止,我的查询不起作用-我可以得到所有的结果,但不只是过滤的结果。
<?php
$x = $_POST['x'];
$y = $_POST['y'];
$term = $_POST['term'];
mysql_connect ("localhost","host","pass") or die (mysql_error());
mysql_select_db ("d_base");
$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) ");
$res2 = mysql_query("SELECT dip FROM my_table WHERE dip IN '%$term%' ORDER BY '%$term%' DESC ");
echo "<table border='1' align='center' cellpadding='5'>";
echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> <th>Diplomacy</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $res, $res2 )) {
// echo out the contents of each row into a table
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['x'] . '</td>';
echo '<td>' . $row['y'] . '</td>';
echo '<td>' . $row['dip'] . '</td>';
echo "</tr>";
// close table>
echo "</table>";
}
?>我不太确定我哪里出了问题,因为我实际上可以得到结果回显,这只是过滤的问题。
发布于 2013-04-07 08:29:19
我看了一下数组,它似乎只接受一个结果集,所以如果我是正确的,您只需要迭代第一个结果集,即未过滤的结果集。
array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
为什么不在一个查询中过滤结果?似乎您正在从单个表(my_table)中获取项,对吗?
这是一个工作的木琴
$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip REGEXP '$term' ORDER BY dip DESC");
while($row = mysql_fetch_array( $res )) {
...
}请注意,您将通过‘AC.26’(或)操作符将不同的术语连接起来。
发布于 2013-04-07 08:25:36
使用:
"SELECT dip FROM my_table WHERE dip IN '%".$term."%' ORDER BY '".$term."' DESC"
或者如果不起作用:
"SELECT dip FROM my_table WHERE dip='".$term."' ORDER BY '".$term."' DESC"
是的,SQL注入:)您非常容易受到攻击。
发布于 2013-04-07 09:11:00
第一次看到您的代码:
while($row = mysql_fetch_array( $res, $res2 )) {是不正确的,因为$res2应该是结果类型,而不是查询的结果。
备选方案如下:
while($row = mysql_fetch_array( $res, MYSQL_ASSOC )) {
while($row = mysql_fetch_array( $res, MYSQL_NUM )) {
while($row = mysql_fetch_array( $res, MYSQL_BOTH )) {我想你是在努力实现这样的目标:
$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'");这意味着这样的代码:
<?php
$x = $_POST['x'];
$y = $_POST['y'];
$term = $_POST['term'];
mysql_connect ("localhost","host","pass") or die (mysql_error());
mysql_select_db ("d_base");
$res = mysql_query("SELECT * FROM my_table WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'");
echo "<table border='1' align='center' cellpadding='5'>";
echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> <th>Diplomacy</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $res, MYSQL_ASSOC )) {
// echo out the contents of each row into a table
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['x'] . '</td>';
echo '<td>' . $row['y'] . '</td>';
echo '<td>' . $row['dip'] . '</td>';
echo "</tr>";
// close table>
echo "</table>";
}
?>为了获得更好的数据库,我认为您应该将这些术语存储在一个单独的表中,然后进行一些连接:
如果您决定这样做,请使用类似于以下方式的查询:
$res = mysql_query("SELECT * FROM my_table mt LEFT JOIN terms t ON (mt.term_id = t.id) WHERE (x BETWEEN $x -75 AND $x +75) AND (y BETWEEN $y -75 AND $y +75) AND dip LIKE '%$term%'");另一个技巧是使用PDO。使用mysql_* -函数并不难。在某种程度上,更容易创建更好、更易读的代码。
https://stackoverflow.com/questions/15860329
复制相似问题