0

We are using following code for setters.

            if (is_numeric($id)) {

            global $db;

            $product = $db->query('SELECT id, name, code, amount FROM products WHERE id = ' . $id . ' LIMIT 1');

            print_r($product);
            echo $product['name'];

            if (!empty($product)) {

                $this->cId = $product['id'];

                $this->cName = $product['name'];

                $this->cDuration = $product['code'];

                $this->cCost = $product['amount'];

            }

            else return false;

        }

    }

But its giving following output with notices...

Array ( [0] => Array ( [id] => 2 [name] => Gaming [code] => 12 [amount] => 20000 ) ) 

Notice: Undefined index: name in root//class.products.php on line 26

Notice: Undefined index: id in root/class.products.php on line 30

What are we doing wrong, please help, thanks.

1
  • Use $product[0]['name'] instead of $product['name']. Your db-abstraction layer expects to return a whole resultset, not just a single row. Commented Mar 29, 2011 at 23:35

3 Answers 3

2

$product actually contains an array of an array, in which you want to access the first one by doing $product[0]

You want to do this instead:

$product[0]['id']

$product[0]['name']

and so on.

Sign up to request clarification or add additional context in comments.

1 Comment

@seoppc It's not a trick if it's the necessary solution. :-P
1

Your array has a nested array, so you'd need to access it using $product[0]['name']. There's still no key 'duration' or 'fee' in there though.

Comments

0

It's a subarray. You can access it with $product[0]['id'] for example `

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.