我正在尝试在数据库中搜索某些数据并显示它。目前,我的代码显示了数据库中的所有数据,而不仅仅是我搜索过的特定数据。有想法该怎么解决这个吗?这是我目前的PHP代码。我也创建了一个表单来输入数据。
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"Animal_Tracker");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
$sql=mysqli_query($link, "Select * FROM Locations ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal Type'];
echo " ";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
答案 0 :(得分:1)
更改
$sql=mysqli_query($link, "Select * FROM Locations ");
类似
$sql=mysqli_query($link, "Select * FROM Locations WHERE Latitude < 50");
所以你只会显示纬度小于50的位置...同样适用于动物类型等......它叫做SQL WHERE语法
答案 1 :(得分:0)
你可以使用fetch_assoc
$sql = "select * from Locations ";
$result = $db_selected -> query($sql);
while ($row = $result -> fetch_assoc()) {
echo $fila['Animal Type']
}
但是,使用mysqi_fetch_array()这是语法
while($row = $result->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
echo $row['Animal type'];
}
查看更多信息php.net