I have a query in which I insert data with json_encode(), and I am trying to receive that data with json_decode(), however, I get an error message that is something like this:

stripslashes() expects parameter 1 to be string, array given in

I have this database table that has about 20 fields. So, this is how I am trying to retrieve the results:

if($q->rowCount() > 0) {
            while($r = $q->fetch(\PDO::FETCH_ASSOC)) {
                $array[] = $r;
            }
            return $array;
        }

The results come back fine, but I can't seem to get pass this stripslashes error. I've also tried json_decode($json_string, true); with no luck. I am hoping that someone can point me into the right direction.

Recommended Answers

All 7 Replies

I should also mention that I am using a framework and my values are formatted like this in the view:

<?php echo clean($this->prog[0]['startDate']);?>

Hi,

For my own personal experimentation potential, what is the purpose or benefit of having the data inserted to db as json encoded Vs. the Conventionals?

There is none. I've been working for 12 hours, and I can't even think straight. I've tried json and serialized, and both give me the same error. If there is an easier way, please enlighten me.

And if my conventional you mean this:

explode(",",$this->prog[0]['acadYearKey'])

I've tried that also, and I still get the same error message.

Member Avatar for diafol

Unless you're storing this data purely in order to pass to a javascript, I can't see the purpose - even then I find it difficult to fathom.

Data in a DB, usually needs to be able to be searched, filtered or sorted (there are exceptions). Cobbling it all together in json format or some sort of stringified array just seems wrong. Which framework are you using? Why is it using stripslashes? There should be no need to use clean with mysqli parameterized binding?

And that is what I don't understand. I am not using stripslashes, but it is giving me that error message.

Actually, you're right. It was being used. I switched from mysqli to pdo and that wasn't clean up in one of my helpers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.