0

I have a created a new table (colours) which has id and hex as columns, all existing ids are kept in the pics table along with the path of the picture, which will go into the new colours table with the many hex values. I am using a class to extract the prominent colours from the picture as array of hex values.

This is the code so far:

$data = $conn->query('SELECT id,pic_path FROM pics WHERE pic_id = 1231');
while($row = $data->fetch()) {
    $image = new ColorsOfImage('images/'.$data['pic_path']);
    $colors = $image->getProminentColors();

    foreach($colors as $key => $val)
    {
        $sql = "INSERT INTO colours (pic_id,colour) VALUES (:pic_id,:colour)";
        $q = $conn->prepare($sql);
        $q->execute(array(':pic_id'=>$data['pic_id'],':colour'=>$val));
    }
}

But I get this error:

Fatal error: Cannot use object of type PDOStatement as array

This is how it should look like this once it's populated:

sample fields

1
  • @Dagon please elaborate? Commented May 21, 2015 at 22:47

2 Answers 2

2

You used $data['pic_id'] when what you wanted was $row['pic_id'].

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

1 Comment

Wow! That was a daft oversight! Thanks
1

This could be done with one single query, no php, no loop required

INSERT INTO colours (pic_id,colour)
SELECT id,pic_path FROM pics
WHERE pic_id = 1231

full details: https://dev.mysql.com/doc/refman/5.1/en/insert-select.html

2 Comments

Oh I see, I only used pic_id to test, really I would use something like WHERE pic_id >1231, would that work?
i only know one way to find out

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.