0

I have a database and it stores discord guilds in it and the discord guilds can have emojis like and • . I am having an issue though when I am trying to do a foreach on the data it just says

PHP Warning: Invalid argument supplied for foreach() in /path/to/my/php/file

I just want to either entirely remove all emojis/unicodes or just be able to show them normally within the foreach statement without a ton of work.

Foreach im using

$stmt = $conn->prepare("SELECT guilds FROM users WHERE discord_id = :discord_id");
$stmt->bindParam(':discord_id', $userdata['discord_id']);
$guildData = $stmt->fecth(PDO::FETCH_ASSOC);
$guildData = json_decode($guildData, true);
var_dump($guildData);

foreach($guildData as $g) {
    echo $g;
}

Sample of the data that is in the column guilds:

[{"id": 1, "guildname": "Server Name "}, {"id": 2, "guildname": "Server Name"}]

I want to either keep them and show all the emojis or just entirely remove all unicode emojis and symbols.

  • You need to post the actual code, not just the error message. – Sammitch May 29 '20 at 00:48
  • Its just a normal foreach ill add in the main post – Severingcastle8 May 29 '20 at 01:10
  • Well the error is because `$guildData` is a string, not an array or other iterable construct. What exactly are you trying to do here? Did you mean to do a `json_decode()` instead of the `json_encode()` you've written there? `true` is not an argument I'd imagine anyone would want to feed in as an encode flag. – Sammitch May 29 '20 at 01:13
  • Ive tried decode, encode nothing works where it actually will display the names with the emojis/unicodes inside of the echo... – Severingcastle8 May 29 '20 at 01:17
  • Let me guess, the emojis all come out looking like `ð¶` or similar? – Sammitch May 29 '20 at 01:23
  • No it just gives me that error the foreach doesn't even work, even just using print_r($userdata['guilds']) doesn't even work. – Severingcastle8 May 29 '20 at 01:29
  • If you do in mysql: `SHOW FULL COLUMNS FROM users where Field='guilds';` what is the exact Collation for it? – Ron May 30 '20 at 01:15
  • https://sevy.i.purplemen.net/2vfCQpnEmMuy.png Is what I get from that. Ive tried changing it back to json as the type but it wont allow me to anymore id prefer to have it as json but just was trying if changing to long text would work since it wouldnt let me put that collation for json type – Severingcastle8 May 30 '20 at 01:19
  • @Severingcastle8 Please [edit] your question to include the current (fixed) source code you have. Also include `var_dump()` lines between loading the data and the `foreach()` loop. Add the output you get from these additional `var_dump()` lines to your question. – Progman May 30 '20 at 08:04
  • I dont have fixed code nothing Ive done actually works for the foreach.... – Severingcastle8 May 30 '20 at 20:18

2 Answers2

0

OK I still haven't been able to figure out what you're actually trying to accomplish, but what is clear is that you don't know what your data looks like and you're fumbling in the dark trying to apply random functions and hoping that something sensible comes out of echo.

Use var_dump(). It will tell you exactly what something is down to the smallest detail. For example, var_dump($userdata['guilds']); will tell you exactly what that variable actually is and let you make an informed decision about what to do with it next.

Eg:

$input_json = '[{"id": 1, "guildname": "Server Name "}, {"id": 2, "guildname": "Server Name"}]';
$decoded = json_decode($input_json, true);

var_dump($input_json, $decoded);

Output:

string(83) "[{"id": 1, "guildname": "Server Name "}, {"id": 2, "guildname": "Server Name"}]"
array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["guildname"]=>
    string(16) "Server Name "
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(2)
    ["guildname"]=>
    string(11) "Server Name"
  }
}

So now I know I should be able to:

foreach( $decoded as $guild ) {
    echo "Guild: " . $guild['guildname'] . "\n";
}

Which gets me:

Guild: Server Name 
Guild: Server Name

and if you want to really up your debugging game you'd want to look at using something like XDebug and an IDE that can plug into it properly.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • If I do what you just did with having the json_deocde() with the true it doesn't work and gives me the error I had shown in the origional post. If It matters I am pulling that json from a database. – Severingcastle8 May 29 '20 at 02:12
  • When I use var_dump() on $userdata['guilds'] which is the guilds json that I pull from the database it just has ?? for the icons... – Severingcastle8 May 29 '20 at 02:16
  • Yea still cant figure this out at all ive tried multiple ways with pulling it from the db but nothing works. – Severingcastle8 May 30 '20 at 00:50
0
$guildData = $stmt->fecth(PDO::FETCH_ASSOC);

Assuming you fix the typo fecth to fetch, this line will return an array. For debugging purposes, when you write

var_dump($guildData);

after that line, you will see something like this:

array(1) {
  ["guilds"]=>
  string(32) "[{"id": 1, "guildname": "Ser...."
}

However, the json_decode() function expects a string, not an array.

To fix it you simply access the guilds field from the array:

$guildData = $stmt->fetch(PDO::FETCH_ASSOC); // returns an array
$jsonData = $guildData['guilds'];            // get the 'guilds' field
$extracted = json_decode($jsonData, true);   // JSON -> array
var_dump($extracted);                        // just for debugging

This will fix the "Invalid argument supplied for foreach()" error message. You also might have to take a look at UTF-8 all the way through to see if you have configured everything the right way to get the content in the correct format.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Yea I had fixed the fetch when I had went over it again, but still the same thing. I had someone walk me through previously with the charset and all the stuff for the database etc. – Severingcastle8 May 30 '20 at 07:43