smanhoff
smanhoff

Reputation: 466

PHP get value of one column in array

I'm writing some data off my MySQL database into an array using columns with php like this:

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $row->id = $row["ID"];
            $row->product = $row["product"];
            $row->aantal = $row["Aantal"];
            $row->price = $row["Price"];
            $od[] = $row;
    }
    } else {
       echo "0 results";
    }

now later I have to use the $row->id and $row->product of the different columns seperatly to get some more data out of another table in MySQL. I've been trying to accomplish this:

$to = 0;
foreach($od as $odt)
{
    $odb= $odt[$to]["ID"];
    $sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
    $to++;

But this doesn't seem to work, I've tried dozen of others but can't seem to get this thing right...

Any solutions or remarks?

EDIT: Array ( [0] => Array ( [ID] => 3 [product] => 10 [Aantal] => 1 [Price] => 3 ) [1] => Array ( [ID] => 4 [product] => 13 [Aantal] => 1 [Price] => 3 ) [2] => Array ( [ID] => 5 [product] => 3 [Aantal] => 3 [Price] => 4 ) )

Upvotes: 1

Views: 1974

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You are doing it wrong, you are saving object in an array and trying the get the data with array, it should be as

foreach($od as $key=>$odt)
{
    $odb= $odt->id;
    $sql = "SELECT `Name` FROM `detail` WHERE `ID` = '$odb'";
}

Upvotes: 3

Related Questions