Php 如果mysql中有一个字段为空,则计算行数

Php 如果mysql中有一个字段为空,则计算行数,php,mysql,Php,Mysql,您好,我的表中有几列,如果其中任何一列为空,则应计为1…同时,如果同一行中有两列或更多列为空…则不应计为2。。。 帮助我查询mysql <?php include("connect.php"); $unit=$_GET['unit']; $chapter=$_GET['chapter']; //$dept=$_GET['dept']; $result=mysql_query("select * from `$unit` where stopic='$chapter'"); if(mysql

您好,我的表中有几列,如果其中任何一列为空,则应计为1…同时,如果同一行中有两列或更多列为空…则不应计为2。。。 帮助我查询mysql

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
    while($row=mysql_fetch_array($result))
    {
        $a=$row['ch1'];
        $b=$row['ch2'];
        $c=$row['ch3'];
        $d=$row['ch4'];
        $e=$row['ans'];
        $f=$row['ques'];
    }
}
else
{
    echo "";
}
?>

我不太明白你的问题。 也许这会有所帮助-您可以使用
isempty()
函数,如果变量为空,该函数将返回true

将名为空的变量设为空,并将其值设为0。然后在for循环中添加以下内容:

if(isempty($a)||isempty($b)||isempty($c)||isempty($d)||isempty($e)||isempty($f))
   $empty++;
试试这个简单的代码

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
$empty_record = 0;
if(mysql_num_rows($result)>0)
{
    while($row=mysql_fetch_array($result))
    {
        $a=$row['ch1'];
        $b=$row['ch2'];
        $c=$row['ch3'];
        $d=$row['ch4'];
        $e=$row['ans'];
        $f=$row['ques'];

        if($a=='' || $b=='' || $c=='' || $d=='' || $e=='' || $f=='')
        {
            $empty_record++;
        }
    }
}
else
{
    echo "";
}
echo $empty_record;

?>

我会尝试以下方法:

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
    $numOfEmpty = 0;
    while($row=mysql_fetch_row($result))
    {
        for($i = 0;$i<count($result);$i++) {
             if ($result[$i] == "") { 
                $numOfEmpty++;
                break;
             }
        }
    }
    echo $numOfEmpty;
}
else
{
    echo "";
}
?>
欢迎使用堆栈溢出。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO。