0

I'm using a php library from https://sourceforge.net/p/wowarmoryapi/home/Home/. It pulls json data from battle.net and stores it in my database for cache reasons.

code to get json from db (I'm not sure if it's still considered json at this point):

$data = $con->query('SELECT Data FROM wa_guilds');

I use the following code to see the data:

foreach($data as $row) {        
    echo "<span style='color:#ff0099'>";
    var_dump($row);
    echo "</span>"; }

which looks like this minus the errors, that's from another code: pink text is the dump

I've tried various methods, mostly from this site to show my data in a way that's easy to read but I always get error.

  1. This is definitely an object. if (is_object($data)) { echo "yay!"; } <--this works
  2. Once I use $decodedjson = json_decode($data); it's no longer an object and I can't seem to print the results to see what it looks like. var_dump($decodedjson) returns NULL

Finally, when I use the following code:

foreach ($data as $da){     
    echo $da['Data']['character']['name'];  }

returns Warning: Illegal string offset 'character'

and:

foreach ($data as $da){     
    echo $da['character']['name'];  }

returns Notice: Undefined index: character

I don't understand what I'd doing wrong, or right. Do I need to somehow turn $data into a string?

NEW CODE

$sth = $con->query('SELECT Data FROM wa_guilds');
$sth->execute();    
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($data as $row) {
    foreach($row as $r) {
        $myData = json_decode($r, true);
        echo "<span style='color:#ff0099'>";
        var_dump($myData['Data']); 
        echo "</span>"; }  } 

NEW ERROR

NULL NULL

3
  • 1
    What gives a var_dump($data); right after the $data = $con->query() line? I'm guessing that you are trying to loop on a query reference, instead of the query results. Commented May 3, 2013 at 14:52
  • @FrostyZ that's all the pink text in the photo. It's so I can read and make sure that someone was actually in the database. Oh yeah, it's in a loop because someone suggested that, I just never changed it. Oh, when I take it out of the loop I get this back: object(PDOStatement)#2 (1) { ["queryString"]=> string(26) "SELECT Data FROM wa_guilds" } Commented May 3, 2013 at 15:01
  • Can you post part of the result of var_dump($data)? Commented May 3, 2013 at 15:38

2 Answers 2

1

From the warning I'm guessing you're using PDO. If $con is your PDO instance representing a connection to a database, try the following:

$sth = $con->prepare('SELECT Data FROM wa_guilds');
$sth->execute();

$data = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach($data as $row) {
    $myData = json_decode($row['Data'], true);
    echo "<span style='color:#ff0099'>";
    // $myData should now be a PHP array which you can access easily
    print_r($myData);
    echo "</span>";
}
Sign up to request clarification or add additional context in comments.

8 Comments

Cool, made those changes. $roster = json_decode($data, true); print $roster; returns Warning: json_decode() expects parameter 1 to be string, array given and if i change it to $roster = json_decode($data['Data'], true); it returns Notice: Undefined index: Data But when I just var_dump as you mentioned above it takes away everything before string(100000).
Can you put on pastebin the result of one print_r($row); instruction in a line between your 2 foreach?
pastebin.com/jLMaJLS3 this is from foreach($data as $row) { echo "<span style='color:#ff0099'>"; var_dump($row['Data']); echo "</span>"; }
print_r($row); changes the beginning to look like this: Array ( [Data] => {"lastModified":1367433943000,"name":"Teh
Check the updated answer, it should print a PHP array now which you can access easily.
|
0

You will need to convert the json string first, I'm not sure how many rows you are expecting from the DB but if it's only one you don't need the loop:

$data = $sth->fetchAll(PDO::FETCH_ASSOC);

$decoded = json_decode($data['Data'], true);

echo "<span style='color:#ff0099'>";
var_dump($decoded);
echo "</span>";

if you need a loop it should work like this:

foreach($data as $d)
{
    $decoded = json_decode($d['Data'], true);

    echo "<span style='color:#ff0099'>";
    var_dump($decoded);
    echo "</span>";
}

5 Comments

that gives me this error: Warning: json_decode() expects parameter 1 to be string, array given Warning: Invalid argument supplied for foreach() $data = $sth->fetchAll(PDO::FETCH_ASSOC); $decoded = json_decode($data, true); foreach($decoded as $row) { echo "<span style='color:#ff0099'>"; var_dump($row['Data']); echo "</span>"; }
My 1st bullet up top says this is coming out as an object and not a string.
try my edited version :) I think $data is in mumeric index $data[0]...$data[n]
Nuts! Notice: Undefined index: Data and NULL for the first part. The loop returns NULL NULL
:S $d['Data'] has to be an array with a string(100000) and that string is a JSON object... I'm lost now! lol

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.