非空的变量,表示未定义的变量

时间:2016-01-06 14:13:32

标签: php mysql phpexcel

请查看以下代码。

        $key = $_SESSION['order_nums'];
        $sqll = "SELECT * FROM `money` WHERE `order` = :key";
        $qq=$con->prepare($sqll);
        $qq->bindvalue(":key", $key);
        $qq->execute();


        $excel2 = PHPExcel_IOFactory::createReader('Excel2007');
        $excel2 = $excel2->load('nTest.xlsx'); // Empty Sheet
        $excel2->setActiveSheetIndex(0);
        $worksheet = $excel2->getActiveSheet();

        $Cno = '7';
        $COno = '7';
        $MSno = '7';
        $CHno = '7';
        $ALno = '7';


        while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   

                $totalCost += $fdata['cost'];


                /*
            $worksheet
                ->setCellValue('A' . $Cno++, $fdata['code'])
                ->setCellValue('D' . $COno++, $fdata['cost'])
                ->setCellValue('E' . $MSno++, $fdata['time'])
                ->setCellValue('F' . $CHno++, $fdata['hourly'])
                ->setCellValue('G' . $ALno++, $fdata['ot']);*/




        }

好的,所以代码中$totalCost += $fdata['cost']所说的部分就是说它是一个未定义的变量。然而,我注释掉了它下面的代码..如果我取消注释它,那就完美了。

1 个答案:

答案 0 :(得分:4)

在使用之前你没有定义$totalCost变量,所以你应该在循环$totalCost = 0;

之外定义它
$totalCost = 0;
while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   

                $totalCost += $fdata['cost'];


                /*
            $worksheet
                ->setCellValue('A' . $Cno++, $fdata['code'])
                ->setCellValue('D' . $COno++, $fdata['cost'])
                ->setCellValue('E' . $MSno++, $fdata['time'])
                ->setCellValue('F' . $CHno++, $fdata['hourly'])
                ->setCellValue('G' . $ALno++, $fdata['ot']);*/




        }

因为此行$totalCost += $fdata['cost'];表示$totalCost = $totalCost + $fdata['cost'];,在这种情况下,它找不到var $totalCost