Fatal error: Call to a member function getFreeFormNumber() on null in C:\qbapi\quickbooks-php-master\docs\

Fatal error: Call to a member function getFreeFormNumber() on null in C:\qbapi\quickbooks-php-master\docs\

我正在研究 Keith Palmer Quickbooks PHP API。一切正常并连接。该示例在数组中查询 return 结果。我想做的是 运行 一个 mysqli 插入语句,它将 运行 通过结果数组并将记录保存在 mysqli table 中。

这是我的代码,它从 Quickbooks 获取记录并将其保存在 mysqli table 中。

$customers = $CustomerService->query($Context, $realm, "SELECT * FROM Customer");


foreach ($customers as $Customer)
{

//First, we will retrieve the ID, and strip out any spaces/characters.  
$id = $Customer->getId();
$str = preg_replace("/[^0-9]/","",$id);

    //Insert customer into customer tables. 
        $sql = "INSERT INTO Customers (CustomerID__kp, CustomerName, CustomerAddress1, CustomerCity, CustomerCounty, CustomerPostcode, CustomerTelephone) 

            VALUES ('".$str."', 
                    '".$Customer->getFullyQualifiedName()."',
                    '".$Customer->getBillAddr(0)->getLine1()."',
                    '".$Customer->getBillAddr(0)->getCity()."',
                    '".$Customer->getBillAddr(0)->getCountrySubDivisionCode()."',
                    '".$Customer->getBillAddr(0)->getPostalCode()."',
                    '".$Customer->getPrimaryPhone(0)->getFreeFormNumber()."'

                    )
                    ON DUPLICATE KEY 
                    UPDATE CustomerName = '".$Customer->getFullyQualifiedName()."',
                    CustomerAddress1 = '".$Customer->getBillAddr(0)->getLine1()."',
                    CustomerCity = '".$Customer->getBillAddr(0)->getCity()."',
                    CustomerCounty = '".$Customer->getBillAddr(0)->getCountrySubDivisionCode()."',
                    CustomerPostcode = '".$Customer->getBillAddr(0)->getPostalCode()."',
                    CustomerTelephone = '".$Customer->getPrimaryPhone(0)->getFreeFormNumber()."'
                    ";

        $conn->query($sql);

}

?>

我的问题是我得到了这个错误:

Fatal error: Call to a member function getFreeFormNumber() on null in C:\qbapi\quickbooks-php-master\docs\

当我运行客户查询时,代码将客户保存在我的quickbooks中,并保存在我的mysqli中。但由于某种原因,我得到了上述错误。

您想先测试值,然后执行函数调用并将结果赋值给变量。然后在查询中使用变量:

foreach ($customers as $Customer)
{

//First, we will retrieve the ID, and strip out any spaces/characters.  
$id = $Customer->getId();
$str = preg_replace("/[^0-9]/","",$id);

// test customer phone number
if(isset($Customer->getPrimaryPhone(0))){
    $CustomerTelephone = $Customer->getPrimaryPhone(0)->getFreeFormNumber();
} else {
    $CustomerTelephone = '';
}

    //Insert customer into customer tables. 
        $sql = "INSERT INTO Customers (CustomerID__kp, CustomerName, CustomerAddress1, CustomerCity, CustomerCounty, CustomerPostcode, CustomerTelephone) 

            VALUES ('".$str."', 
                    '".$Customer->getFullyQualifiedName()."',
                    '".$Customer->getBillAddr(0)->getLine1()."',
                    '".$Customer->getBillAddr(0)->getCity()."',
                    '".$Customer->getBillAddr(0)->getCountrySubDivisionCode()."',
                    '".$Customer->getBillAddr(0)->getPostalCode()."',
                    '".$CustomerTelephone."'

                    )
                    ON DUPLICATE KEY 
                    UPDATE CustomerName = '".$Customer->getFullyQualifiedName()."',
                    CustomerAddress1 = '".$Customer->getBillAddr(0)->getLine1()."',
                    CustomerCity = '".$Customer->getBillAddr(0)->getCity()."',
                    CustomerCounty = '".$Customer->getBillAddr(0)->getCountrySubDivisionCode()."',
                    CustomerPostcode = '".$Customer->getBillAddr(0)->getPostalCode()."',
                    CustomerTelephone = '".$CustomerTelephone."'
                    ";

        $conn->query($sql);

}

警告!

Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string不安全!