0

I am having trouble saving this array to MySQL.

$arr = array("red","green","blue");

json_encode($arr);

$results = $db->query("UPDATE settings SET data='$arr' WHERE type='testing'");

The database field is updated and just shows "Array"

3 Answers 3

3

Just pass it into variable as. Yours not working because you have encoded the array but didn't passed the encoded value

$arr = json_encode($arr);

$results = $db->query("UPDATE settings SET data='$arr' WHERE type='testing'");
Sign up to request clarification or add additional context in comments.

1 Comment

You are using a wrong name as variabele and thus you suggest that $arr is an array which is not the fact. You should change that to a name like $json.
0

When you read the description of json_encode you can see the sentence:

Returns a string containing the JSON representation of value.

So you have to fetch the result and save them first in a variable. You can use the same and overwrite them something like this:

$arr = array("red","green","blue");
$arr = json_encode($arr);
$results = $db->query("UPDATE settings SET data='$arr' WHERE type='testing'");

Then it should work.

Comments

0

Because you have discarded the value of json_encode(), save that by changing this line json_encode($arr); to

$arr = json_encode($arr);

Or, if you want to preserve your $arr for future use, change your query like,

$results = $db->query("UPDATE settings SET data='{json_encode($arr)}' WHERE type='testing'");

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.