0

嗨,我有几个 php 类。我正在传递一个到 MySql 数据库的连接。当我创建第一个类的实例并尝试使用 getinvoicedetails() 方法调用发票类时,我收到错误“命令不同步;您无法运行此命令”。

任何帮助将不胜感激。

这是代码:

    <?php //Invoicectrl class
    class invoicectrl
{
    // define properties
     public $invoices = Array();
     public $connection;
     public $userID;
     public $ownerID;

    // constructor
    public function __construct($userIDin,$ownerIDin) {
        $this->userID = $userIDin;
        $this->ownerID = $ownerIDin;

    }

     //function to set connection to db
    public function setconnection($conn){
        $this->connection = $conn;
    }
    public function getinvoicelist(){
        $res = Array();
        $invIDs = Array();

        //Get list of invoicenumbers and invoiceIDs
        $queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
        $result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);

        // GOING THROUGH invoices and getting there data
        $o=0;
        while($row = $result->fetch_assoc()) {
           $invIDs[$o]['ID'] = $row['invoiceID'];
           $invIDs[$o]['NR'] = $row['invoicenumber'];                               }
                   $result->free();

                   //Set each invoice own info
                   for($i=0;$i<=count($invIDs)-1;$i++){
                      $res[$i] = new invoice($invIDs['ID'],$invIDs['NR']);
                      $res[$i]->setconnection($this->connection);
           $res[$i]->getinvoicedetails();
                   } 

        return $res;
    }

}

?>

这是发票类

    <?php //Invoice class
    class invoice
{
    // define properties
     public $invoiceID;
     public $invoicenumber;

     public $itemIDs=Array();
     public $items= Array();

     public $customerID;
     public $cust_company;
     public $cust_name;
     public $cust_email;
     public $cust_adress1;
     public $cust_adress2;
     public $cust_adress3;

     private $connection;

    // constructor
    public function __construct($invoiceIDin,$invoicenumberin) {
        $this->invoiceID = $invoiceIDin;
        $this->invoicenumber = $invoicenumberin;
    }

    //function to set connection to db
    public function setconnection($conn){
        $this->connection = $conn;
    }
        //sets all invoices details
    public function getinvoicedetails(){

                  //Get customer on invoice
                    $queryd = "select customerID FROM Accounting_Invoice_Customer where invoiceID =".$this->invoiceID."";
        $result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
                   while($row1 = $result->fetch_assoc()) {
            $this->customerID = $row1['customerID'];
                   }
                   $result->free();

                    //Get list of items on invoice
        $queryd = "call sp_getItems(".$this->invoiceID.")";
        $result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
        $o=0;
                   while($row2 = $result->fetch_assoc()) {
            $this->items[$o]['itemID'] = $row2['itemID'];

            //$this->items[$o]['itemnumber'] = $row['itemnumber'];
            //$this->items[$o]['itemdescription'] = $row['itemdescription'];
            //$this->items[$o]['itempricewotax'] = $row['itempricewotax']; 
            $o++;
        }       
                   $result->free(); 
    }
}
?>
4

1 回答 1

0

当您运行 SQL 请求时,您必须通过整个结果或关闭游标,然后再发送另一个请求。

最好的办法是首先获取整个结果,然后使用 foreach 循环遍历它,以便能够发送第二个请求。此外,在您的 sql 查询中使用 INNER JOIN 应该是一个好主意。

$queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
$invIDs = $result->fetch_all();
$res = array();
foreach($invIDs as $inv){
     $inv = new invoice($invIDs['invoiceID'],$invIDs['invoiceNumber']);
     $inv->setconnection($this->connection);
     $inv->getinvoicedetails();
     $res[] =$inv;
}          
于 2013-01-30T14:54:58.147 回答