0
 <?php
 $soil_ph = $_POST['soilph'];
 $query = "select ph_id,ph_name,ph_from,ph_to from tbl_soilph 
 where '$soil_ph' between ph_from and ph_to";

 $result = mysql_query($query);

 while($row = mysql_fetch_array($result)) 
{
if ($row == 0)
{
echo 'Invalid or out of range';
}
else
{
$ph = $row['ph_name'];
echo $row['ph_name'];
}
}
?>  

---回声不起作用@($row = 0)---有人可以帮助我吗?上面的代码工作正常,它给出了结果,但是当没有数据时它不显示消息“无效输入”?

4

4 回答 4

2

首先,在数据库查询中使用变量之前,您应该始终对其进行转义(除非您使用准备好的语句,您应该这样做):

$soil_ph = $_POST['soilph'];
$query = "SELECT ph_id, ph_name, ph_from, ph_to 
    FROM tbl_soilph 
    WHERE '" . mysql_real_escape_string($soil_ph) . "' BETWEEN ph_from AND ph_to";

$result = mysql_query($query);

要检查您是否有任何结果,您应该mysql_num_rows()在确保查询没有失败后使用:

if ($result && mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
        // do your stuff
    }
} else {
    // aww, nothing there
}

进一步阅读:MySQLiPDO

于 2012-11-16T06:15:15.970 回答
0

检查此代码,您已更改循环方式

if (mysql_num_rows($result) < 1) {

    echo 'Invalid or out of range';

}else{

     while($row = mysql_fetch_array($result)){

         $ph = $row['ph_name'];
         echo $row['ph_name'];
     }
}
于 2012-11-16T06:06:16.240 回答
0
$soil_ph = $_POST['soilph'];
$query = "select ph_id,ph_name,ph_from,ph_to from tbl_soilph 
where '$soil_ph' between ph_from and ph_to";

$result = mysql_query($query);
if($result && mysql_num_rows($result)) {

 while() {

 }
} else {
  echo 'invalid input';
}
于 2012-11-16T05:56:08.843 回答
-1

实际上 mysql_fetch_array 函数在每次执行时返回一个单维数组。你可以这样做:

if($results){
echo 'No Results';
}else{
while($row = mysql_fetch_array($result)) 
{
if(!empty($row)){ 
echo $row['ph_name'];
}else{
echo 'invalid';
}
}
于 2012-11-16T04:51:34.753 回答