0

So I have an array:

$seperatedunits = [379234748138258434, 544641634696953856, 544641634696953856, 1010101010101010]

I loop through the array:

$result = $pdo->query("SELECT * FROM activecalls");

foreach ($result as $row) {

  $attachedunits = $row['attachedunits'];
  $seperatedunits = explode(",",$attachedunits);
  
  foreach ($seperatedunits as $value)
  {
    $getidentifier = $pdo->query("SELECT * FROM users WHERE discordid LIKE '%$value%'");
    foreach ($getidentifier as $row2)
    {
      echo $row2['identifier'];
    }
  }

}

The output I'm getting with this is just 1 name:

[1L-01] Hamz

The desired output is to have 4 names come up corresponding to the id in the array.

How would I fix this so it displays all 4?

Hamz
  • 44
  • 6
  • 3
    That's a string, not an array – iainn Feb 19 '21 at 17:38
  • 4
    If you're really trying to pull this on a string, you should be getting an "Invalid argument supplied for foreach" warning. If you're not seeing it, consider turning on [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Feb 19 '21 at 17:41
  • I'm curious how this can give you _any_ name in it's current form. – M. Eriksson Feb 19 '21 at 17:42
  • `foreach (preg_split('/,\s*/', $seperatedunits) as $value)` to split that string on the comma and possible whitespace – Michael Berkowski Feb 19 '21 at 17:42
  • @MagnusEriksson if I had to guess, it would be something like the string evaluating to int zero and matching a record. – Michael Berkowski Feb 19 '21 at 17:43
  • Wait sorry my bad, so the variable comes with commas in a string format, and i explode it like this and it lets me loop it through and it displays each of them as $value $seperatedunits = explode(",",$attachedunits); – Hamz Feb 19 '21 at 17:44
  • @MichaelBerkowski - Weird with a discord id that matches with 0 though. – M. Eriksson Feb 19 '21 at 17:45
  • 2
    @Hamz when you explode, are you exploding on the comma only? Your input string is `value, value, value` not `value,value,value` so most of your elements would end up with a leading space – Michael Berkowski Feb 19 '21 at 17:46
  • @Hamz - Please show us all the code you have related to this. – M. Eriksson Feb 19 '21 at 17:46
  • @MagnusEriksson maybe a faulty explode leaving a leading space on most of them – Michael Berkowski Feb 19 '21 at 17:46
  • @MichaelBerkowski - Well, now when the OP says there are more code, who knows what's going on. – M. Eriksson Feb 19 '21 at 17:46
  • Okay, Ill grab it all, the $attachedunits is coming from a column in a table. – Hamz Feb 19 '21 at 17:48
  • Just updated the code, if you need more information let me know as I might be explaining it bad – Hamz Feb 19 '21 at 17:52
  • Okay so I think I got it working, I just had to get rid of the leading space like you said :) Thanks for the help! – Hamz Feb 19 '21 at 17:59
  • If you're having to explode a text string so you can loop through it to run further repeated queries then your table design is fundamentally wrong. – Tangentially Perpendicular Feb 19 '21 at 19:16

0 Answers0