当数据库没有输出时如何返回任何内容

时间:2017-12-29 03:33:47

标签: php mysqli

我知道如果数据库没有结果,怎么回事?当有结果时,它显示正常,但是当没有结果时,会发生这种情况!我附上了一张图片,以便在提交数据时显示结果! 如果条件与我想要的结果不匹配,我如何完全不显示任何内容?

谢谢你的帮助!

Click here for an image of my result

if(isset($_POST['submit'])){ 
$rap=$_POST['AtRap'];




$percent = (15/100);
$diff = $percent * $rap ;
$a=   $rap - $diff;
$b=   $rap + $diff;
$query = mysqli_query($con,"SELECT Max(Shape), Max(Clarity), Max(Colour), Max(Carat), Max(RapNinety), Max(AtRap) FROM Rapaport WHERE (SELECT AtRap BETWEEN '$a' AND '$b') AND (Colour='J' and Clarity='VS2' and Shape='Round') LIMIT 1 ") ;

$query2 = mysqli_query($con,"SELECT Max(Shape), Max(Clarity), Max(Colour), Max(Carat), Max(RapNinety), Max(AtRap) FROM Rapaport WHERE (SELECT AtRap BETWEEN '$a' AND '$b') AND (Colour='G' and Clarity='VS1' and Shape='Round') LIMIT 1 ") ;

$query3 = mysqli_query($con,"SELECT Max(Shape), Max(Clarity), Max(Colour), Max(Carat), Max(RapNinety), Max(AtRap) FROM Rapaport WHERE (SELECT AtRap BETWEEN '$a' AND '$b') AND (Colour='F' and Clarity='VVS2' and Shape='Round')  LIMIT 1") ;

$query4 = mysqli_query($con,"SELECT Max(Shape), Max(Clarity), Max(Colour), Max(Carat), Max(RapNinety), Max(AtRap) FROM Rapaport WHERE (SELECT AtRap BETWEEN '$a' AND '$b') AND (Colour='D' and Clarity='VVS1' and Shape='Round')  LIMIT 1") ;






$count4 = mysqli_num_fields($query4);
if($count4 == 0){
    echo "<script type='text/javascript'>alert('No Results')</script>";
}else{
    while($row4 = mysqli_fetch_array($query4)) {
        $shape = $row4['Max(Shape)'];
        $clarity = $row4['Max(Clarity)'];
        $colour = $row4['Max(Colour)'];
        $carat = $row4['Max(Carat)'];
        $rap90 = $row4['Max(RapNinety)'];
        $atrap = $row4['Max(AtRap)'];

        $output .= " <br /><br />Shape: <b>$shape</b>  &nbsp;&nbsp; Clarity: $clarity  &nbsp;&nbsp; Colour: $colour &nbsp;&nbsp; Carat: &nbsp;$carat <br /><br /> Price Quote:  <br /><br /> <b>$$rap90 - $$atrap</b> (in SGD)<br /><br /> ";
    }

}

$count3 = mysqli_num_fields($query3);
if($count3 == 0){
    echo "<script type='text/javascript'>alert('No Results')</script>";
}else{    

    while($row3 = mysqli_fetch_array($query3)) {
        $shape = $row3['Max(Shape)'];
        $clarity = $row3['Max(Clarity)'];
        $colour = $row3['Max(Colour)'];
        $carat = $row3['Max(Carat)'];
        $rap90 = $row3['Max(RapNinety)'];
        $atrap = $row3['Max(AtRap)'];

        $output .= " <br /><br />Shape: <b>$shape</b>  &nbsp;&nbsp; Clarity: $clarity  &nbsp;&nbsp; Colour: $colour &nbsp;&nbsp; Carat: &nbsp;$carat <br /><br /> Price Quote:  <br /><br /> <b>$$rap90 - $$atrap</b> (in SGD)<br /><br /> ";
        }

}




$count2 = mysqli_num_fields($query2);
if($count2 == 0){
    echo "<script type='text/javascript'>alert('No Results')</script>";
}else{

    while($row2 = mysqli_fetch_array($query2)) {
        $shape = $row2['Max(Shape)'];
        $clarity = $row2['Max(Clarity)'];
        $colour = $row2['Max(Colour)'];
        $carat = $row2['Max(Carat)'];
        $rap90 = $row2['Max(RapNinety)'];
        $atrap = $row2['Max(AtRap)'];


        $output .= " <br /><br />Shape: <b>$shape</b>  &nbsp;&nbsp; Clarity: $clarity  &nbsp;&nbsp; Colour: $colour &nbsp;&nbsp; Carat: &nbsp;$carat <br /><br /> Price Quote:  <br /><br /> <b>$$rap90 - $$atrap</b> (in SGD)<br /><br /> ";
    }

}

$count = mysqli_num_fields($query);
if($count == 0){
    echo "<script type='text/javascript'>alert('No Results')</script>";

}else{
    while($row = mysqli_fetch_array($query)) {
        $shape = $row['Max(Shape)'];
        $clarity = $row['Max(Clarity)'];
        $colour = $row['Max(Colour)'];
        $carat = $row['Max(Carat)'];
        $rap90 = $row['Max(RapNinety)'];
        $atrap = $row['Max(AtRap)'];

        $output .= " <br /><br />Shape: <b>$shape</b>  &nbsp;&nbsp; Clarity: $clarity  &nbsp;&nbsp; Colour: $colour &nbsp;&nbsp; Carat: &nbsp;$carat <br /><br /> Price Quote:  <br /><br /> <b>$$rap90 - $$atrap</b> (in SGD)<br /><br /> ";
    }


    }
}

2 个答案:

答案 0 :(得分:0)

您使用mysqli_num_fields获取列数。

使用mysqli_num_rows获取rows的数量。

答案 1 :(得分:0)

您正在使用mysqli_num_fields,即使表为空,也会返回结果列数。使用mysqli_num_rows代替返回输出行数。

示例:如果一个表有5列且内部没有数据。

mysqli_num_fields()返回5.

mysqli_num_rows()返回0。

这就是为什么你得到这样的输出。

在下面的代码中,如果没有结果,$ count将为零,并将执行if部分代码。如果有数据则执行else部分。

尝试在代码的每个if ...中使用这段代码。

$count = mysqli_num_rows($query);
if($count == 0){
    echo "<script type='text/javascript'>alert('No Results')</script>";

}else{
    //Rest of code 
}