-3

How I can get this query to create another string from the outputted rows?

You see, '$convert' contains 2 values from my limits table which are 1 and 38, when I echo $convert, I end up with a string which is: "138" Does anyone know how I can convert it to a string which would be: "1, 38"? with a ',' in between each ID. I need it to be like this so I can put it into another query as a condition.

I have tried functions like Implode() and split() but I'm having no luck. Any help would be appreciated. Code below.

$SQL_Ban = "SELECT banned_ItemID FROM limits WHERE UserID = 2;";
$banResult = mysqli_query($connection, $SQL_Ban);
                
if(mysqli_num_rows($banResult) > 1) {
    while($row = mysqli_fetch_array($banResult)) {
    
        $convert = $row['banned_ItemID'];

        var_dump($convert);

OUTPUT: string(1) "1" string(2) "38"

The output shows that they are in two (2) strings, but I need a ',' to be in the middle of them.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Harry Wok
  • 9
  • 5
  • 1
    `$convert` contains a single value. I'm not sure what you mean. Please explain the problem better. Show what you tried already – Dharman Apr 30 '21 at 12:18
  • Sounds like you may need `IN` for the second query (https://stackoverflow.com/questions/907806/passing-an-array-to-a-query-using-a-where-clause for example) or perhaps be able to create just 1 query in the first place. – Nigel Ren Apr 30 '21 at 12:21
  • A way to do what you are _asking for_ (although that is likely not the correct solution to your actual problem, see Nigel’s previous comment) - Add the values into an array inside the loop, use `implode` to join the array elements together with ` OR ` in between them afterwards. – CBroe Apr 30 '21 at 12:25
  • Then you need to concatenate both strings. – Dharman Apr 30 '21 at 12:35
  • @Dharman Yes, thats the point of the question. How? But i've changed OR to a ',' (comma) now – Harry Wok Apr 30 '21 at 12:38
  • https://stackoverflow.com/questions/8336858/how-to-combine-two-strings-together-in-php Since you are in a loop, you can prepare an empty string before the loop and then concatenate into it using `$string .= $another . ' string';` etc. – Dharman Apr 30 '21 at 12:40
  • @Dharman Yes, but the $convert comes out as "138" as stated in the question. Although the var dump shows them as two different strings, how do I define each one. And also. This method may not work. As there may not always be exactly two ID's. – Harry Wok Apr 30 '21 at 12:48
  • What I said before will work. However, a simpler way is to collect the strings into an array and then `implode()`. – Dharman Apr 30 '21 at 12:50
  • Does this answer your question? [How to combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-to-combine-two-strings-together-in-php) – Don't Panic May 01 '21 at 07:09

1 Answers1

0

Collect all strings into an array and then merge the array using implode()

$SQL_Ban = "SELECT banned_ItemID FROM limits WHERE UserID = 2;";
$banResult = mysqli_query($connection, $SQL_Ban);
                
if(mysqli_num_rows($banResult) > 1) {
    $strings = []; // empty array
    foreach($banResult as $row) {
        // append into an array
        $strings[] = $row['banned_ItemID'];
    }
    // merge the elements using ', ' as a separator
    echo implode(', ', $strings);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135