PHP-致命错误:在非对象上调用成员函数dbRows()

时间:2017-04-03 10:25:55

标签: php mysql phpexcel

我尝试从excel格式的复选框选择中下载数据库中的记录。我收到致命错误:

  

在$ res =上调用非对象上的成员函数dbRows()   $ DB-> dbRows($ SQL);

下载的excel文件。

$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to mysql:<br>" . mysql_error() . "<br>" . mysql_errno());

$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());


//$sql = "Select * from $DB_TBLName WHERE Delivery_no = '$_REQUEST[Delivery_no]'" ;


    $checks = implode(",",array_map('strval', $_REQUEST['check']));

    if(isset($_POST['submit']))
            {
        if(!empty($_POST['check'])) 
                    {


            global $DB; 
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=download.xls");
            header('Cache-Control: max-age=0');
            require_once 'protectExcel/Classes/PHPExcel.php';
            require_once 'protectExcel/Classes/PHPExcel/IOFactory.php';


            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
            $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);

            $objPHPExcel->setActiveSheetIndex(0);
            $objPHPExcel->getActiveSheet()->setTitle('Favorite');
            /*$objPHPExcel->getActiveSheet()->mergeCells('A1:R1');*/
            $objPHPExcel->getActiveSheet()->setCellValue('A1','id');
            $objPHPExcel->getActiveSheet()->setCellValue('B1','Brand');
            $objPHPExcel->getActiveSheet()->setCellValue('C1','Bill_date');
            $objPHPExcel->getActiveSheet()->setCellValue('D1','Invoice_no');
            $objPHPExcel->getActiveSheet()->setCellValue('E1','Bill_to_party');
            $objPHPExcel->getActiveSheet()->setCellValue('F1','Product' );
            $objPHPExcel->getActiveSheet()->setCellValue('G1','Category');
            $objPHPExcel->getActiveSheet()->setCellValue('H1','Category_product');
            $objPHPExcel->getActiveSheet()->setCellValue('I1','Barcode');
            $objPHPExcel->getActiveSheet()->setCellValue('J1','Size');
            $objPHPExcel->getActiveSheet()->setCellValue('K1','Shade');
            $objPHPExcel->getActiveSheet()->setCellValue('L1','MRP');
            $objPHPExcel->getActiveSheet()->setCellValue('M1','Quantity');
            $objPHPExcel->getActiveSheet()->setCellValue('N1','Item_code');
            $objPHPExcel->getActiveSheet()->setCellValue('O1',' Material_desc');
            $objPHPExcel->getActiveSheet()->setCellValue('P1',' CD');
            $objPHPExcel->getActiveSheet()->setCellValue('Q1','TD');
            $objPHPExcel->getActiveSheet()->setCellValue('R1','SP.CD');
            $objPHPExcel->getActiveSheet()->setCellValue('S1','Rate');
            $objPHPExcel->getActiveSheet()->setCellValue('T1','Net_amount');
            $objPHPExcel->getActiveSheet()->setCellValue('U1','Delivery_no');

            $row =2;

            $sql = "Select * from $DB_TBLName WHERE Delivery_no IN ($checks)" ;
            echo $sql;

            $result= @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());


            $res = $DB->dbRows($sql);



            foreach($res  as $d) {

                $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $d['id']);
                $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $d['Brand']);
                $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $d['Bill_date']);
                $objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $d['Invoice_no']);
                $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $d['Bill_to_party']);
                $objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $d['Product']);
                $objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $d['Category']);
                $objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $d['Category_product']);
                $objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $d['Barcode']);
                $objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $d['Size']);
                $objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $d['Shade']);
                $objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $d['MRP']);
                $objPHPExcel->getActiveSheet()->setCellValue('M'.$row, $d['Quantity']);
                $objPHPExcel->getActiveSheet()->setCellValue('N'.$row, $d['Item_code']);            
                $objPHPExcel->getActiveSheet()->setCellValue('O'.$row, $d['Material_desc']);
                $objPHPExcel->getActiveSheet()->setCellValue('P'.$row, $d['CD']);            
                $objPHPExcel->getActiveSheet()->setCellValue('Q'.$row, $d['TD']);
                $objPHPExcel->getActiveSheet()->setCellValue('R'.$row, $d['SP.CD']);
                $objPHPExcel->getActiveSheet()->setCellValue('S'.$row, $d['Rate']);
                $objPHPExcel->getActiveSheet()->setCellValue('T'.$row, $d['Net_amount']);
                $objPHPExcel->getActiveSheet()->setCellValue('U'.$row, $d['Delivery_no']);

                $row++;
            }
        } 

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

        exit;   
    }

1 个答案:

答案 0 :(得分:1)

PHP代码区分大小写。

你有var $Db = @mysql_select_db(...

但是你试着把它称为global $DB;我猜是null。因为它没有定义。

试试global $Db; 或者将初始实例化更改为 $DB = @mysql_select_db(...

希望这会有所帮助。

警告停止使用mysql_个功能。它们已被弃用。了解mysqli和/或PDO函数。