Welcome Guest, Not a member yet? Register   Sign In
Trouble with database return value
#1

CodeIgniter 4, I have the following Controller code:

PHP Code:
$product_tier $this->SiteModel->getProductTierByPCUID($site->id$p->uid); 


with the following model code:


PHP Code:
public function getProductTierByPCUID($site_id$pcuid) {
    $sql 'SELECT * FROM site_product_tier WHERE site_id = ? AND pcuid = ?';
    $query $this->db->query($sql, array($site_id$pcuid));
    return $query->getRow();



if I echo out $product_tier immediately below the call, I get this:


PHP Code:
{
    "id""256",
    "site_id""22",
    "product""0",
    "tier""0",
    "posid""01",
    "pcuid""01",
    "psprc""0.000",
    "snprc""0.000",
    "snsrc""",
    "pstat""",
    "created""1699999047",
    "updated""1699999047",
    "deleted""0"



gettype($product_tier) gives me object.

However, if I run this:


PHP Code:
$used_product_tiers = array();

$product_tier $this->SiteModel->getProductTierByPCUID($site->id$p->uid);

if (!
in_array($product_tier->id$used_product_tiers)) {




I get the error:


PHP Code:
{
    "title""ErrorException",
    "type""ErrorException",
    "code"500,
    "message""Attempt to read property \"id\" on null",
    "file""/var/www/vhosts/dev.pricevision.live/httpdocs/app/Controllers/Api.php",
    "line"553,



Line 553 is the if statement.

What am I missing?

If I set product_tier->id to like 300 the code works, but it can;t seem to read $product_tier->id in the if statement.
Reply
#2

$query->getRow() may return null. Check the value before you use it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB