0

ほぼ動作するようになりました。表で結果を取得できますが、何らかの理由で、結果が正しくありません。

座標 x 100 & y 100 を検索します

x 74-125 & y 74-125 ではなく、x 1-25 & y 1-25 のみの結果が得られます

<?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 に答える