-1
    // This gets all the users that are active
// The limit is completely random, it is set to 2 for this example
$sql = <<<SQL
SELECT *
FROM `accounts`
WHERE active = 1
LIMIT 2
SQL;

if(!$getaccounts = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

while ($row = $getaccounts->fetch_assoc()) {
    $getid = $row["id"].',';

    $getid = substr($getid, 0, -1);

    $getusername = $row["username"];
    $getpassword = $row["password"];

    echo $getid;
    echo $getusername."<br>";
    echo $getpassword."<br>";
}

I know this hasn't been prepared but I am not using it for anything other than personal use.

I cannot understand why this is not getting rid of the last comma?

The output may be something like "32,14,"

And I want to get rid of the last comma by using the "substr" function.

But the output that that I get from $getid is "3214" (It gets rid of all the commas instead of just the last one.

I need it to output "32,14" but it's not working?

Could someone please tell me where I am going wrong?

If I do rtrim, it does the same thing and gets rid of all the commas! I am going to update something in the database using the ids, and that is why I need to get rid of the last comma

And I know this code is not secure, I am not using it for anything other than personal use and I was hoping someone could help me figure this out, I have been attempting it for days, it seems really simple and I bet I am missing something really stupid!

MoltoGames
  • 17
  • 1
  • 8

2 Answers2

2

You have a XY Problem.
You want to concat all the id's into a comma-seperated string.
Here's a much easier solution by adding the items to an array and then implode().

<?php 
// rest of your code 
$ids = Array();

while ($row = $getaccounts->fetch_assoc()) {
   $ids[] = $row["id"];
   $getusername = $row["username"];
   $getpassword = $row["password"];

   echo $getusername."<br>";
   echo $getpassword."<br>";
}
echo "ids: " . implode(",",$ids);
Jeff
  • 6,895
  • 1
  • 15
  • 33
0

You should write code like..

  $getid = ""; 
  while ($row = $getaccounts->fetch_assoc()) {
      $getid .= $row["id"].',';
  }
 $getid = rtrim($getid,',');

 $q = " UPDATE accounts SET active = '0' WHERE id IN ($getid) "; 
Dharmendra Singh
  • 1,186
  • 12
  • 22
  • The numbers like "32,14," are received from the database and if I did rtrim($getid,','); it would remove all of the commas, and the result I would be left with would be "3214" and I want it to output "32,14" Thanks! – MoltoGames Jul 15 '17 at 16:38
  • I have no idea why this isn't working? Why is it removing all of the commas? – MoltoGames Jul 15 '17 at 16:38
  • I want to put these ids in a database to do an update. My SQL code would then be UPDATE accounts SET active = '0' WHERE id IN ($getid) and $getid should be "1,2,3,4,5,6,7" but instead using the code I have getid is "1234567" which won't work? – MoltoGames Jul 15 '17 at 16:42
  • you need $getid outside of the loop or inside the loop. I have updated my code.. – Dharmendra Singh Jul 15 '17 at 16:42
  • I just tried your code, and the output I got was "9" I have a limit of 2, there should be a second result but it isn't there? – MoltoGames Jul 15 '17 at 16:47