0

现在几乎可以工作了。我可以在表格中得到结果,但由于某种原因,它们并不完全正确。

我搜索坐标 x 100 & y 100

我得到的结果只有 x 1-25 & y 1-25 而不是 x 74-125 & y 74-125

<?php

$x = $_POST['x'];
$y = $_POST['y'];

mysql_connect ("localhost","user","pass")  or die (mysql_error());
mysql_select_db ("db");


$res = mysql_query("select * FROM table WHERE (x between $x-25 AND $x+25) AND (y BETWEEN $y-25 AND $y+25)");

 echo "<table border='1' align='center' cellpadding='5'>";
    echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $res )) {

            // echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['cityname'] . '</td>';
    echo '<td>' . $row['x'] . '</td>';
echo '<td>' . $row['y'] . '</td>';
    echo "</tr>";
    } 

      // close table>
    echo "</table>";
?>
4

3 回答 3

1

试试这个:

$query="SELECT * FROM cityname
WHERE (x between".$_POST['search']."-25 AND ".$_POST['search']."+25)
AND (y between".$_POST['search']."-25 AND ".$_POST['search']."+25)";
于 2013-04-06T10:11:28.017 回答
0

当我给你这个的时候,请记住,以你现在的方式来做这件事会让你更容易接触到 SQL 注入,并且你应该真正使用 PDO 或类似的方法来准备语句。话虽如此:

$x = (intval)$_POST['fld_x'];
$y = (intval)$_POST['fld_y'];

$res = mysql_query("select * FROM my_dbase WHERE (x between $x-25 AND $x+25) AND (y BETWEEN $y-25 AND $y+25)");
$data = array();
while($row = mysql_fetch_assoc($res)) {
    $data[] = $row;
}

然后做你想做的事$data

示例SQL 小提琴

编辑

为确保数据库连接正确,请在需要时更改/添加以下内容:

if(!mysql_select_db ("dbase")) {
    die(mysql_error());
}

$res = mysql_query...放:

if(!$res) {
    die("Query Failed: ".mysql_error());
}

编辑

根据您目前拥有的完整代码:

您之前将 HTML 显示为:

<form action="xysearch.php" method="get">
<label>X Coord
<input type="text" name="fld_x" />
</label>
<label>Y Coord
<input type="text" name=fld_y" />
</label>
<input type="submit" value="Search" />
</form>

PHP将是:

<?php

$x = (intval)$_POST['fld_x'];
$y = (intval)$_POST['fld_y'];

mysql_connect ("localhost","user","pass")  or die (mysql_error());
mysql_select_db ("db");


$res = mysql_query("select * FROM table WHERE (x between $x-25 AND $x+25) AND (y BETWEEN $y-25 AND $y+25)");

 echo "<table border='1' align='center' cellpadding='5'>";
 echo "<tr> <th>City Name</th> <th>X</th> <th>Y</th> </tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $res )) {
    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['cityname'] . '</td>';
    echo '<td>' . $row['x'] . '</td>';
    echo '<td>' . $row['y'] . '</td>';
    echo "</tr>";
} 

// close table>
echo "</table>";
?>
于 2013-04-06T10:12:13.800 回答
0

您需要使用毕达哥拉斯定理,因为您希望圆的半径为 25

所以查询将是

SELECT cityname, x, y FROM my_database
WHERE SQRT((x - centre_x) * (x - centre_x) + (y - centre_y) * (y - centre_y)) <=25

其中 center_x, center_y 是你输入的位置

于 2013-04-06T10:22:29.230 回答