<?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)---有人能帮帮我吗? 上面的代码工作正常,它给出了结果,但是当没有数据时,它不显示消息“无效输入”?
答案 0 :(得分: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
}
答案 1 :(得分: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';
}
答案 2 :(得分: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'];
}
}
答案 3 :(得分:-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';
}
}