0

I am Trying to Save Foreach Loop value in a Variable as comma separated.For this I have below code so far.It is getting all the values in a list but without Comma. How can I insert Comma after each id

$stmt = $con->query($sql);  
$tmp = '';

foreach ($stmt as $row)
{
  $user_id=$row['user_id'];
  $tmp .= $row['user_id']; 
}

echo $tmp;
roy
  • 131
  • 1
  • 10
  • 3
    Well, have you tried actually putting a comma in the string? You seem to know how to concatenate string, so I'm not really sure what problem you're having. You haven't made any _attempt_ to add a comma here. – Patrick Q Mar 30 '20 at 14:38
  • https://www.php.net/manual/en/function.implode.php – AbraCadaver Mar 30 '20 at 14:39
  • I tried like this ",".$row['user_id'] but not working – roy Mar 30 '20 at 14:41
  • @AbraCadaver Would have to be combined with https://www.php.net/manual/en/function.array-column.php – Patrick Q Mar 30 '20 at 14:42
  • `$tmp .= $row['user_id'] . ',';` then after the loop `trim($tmp, ',');` to remove the trailing comma – RiggsFolly Mar 30 '20 at 14:42
  • A better option would be `implode` or possibly even `group_concat` at the DB level. – user3783243 Mar 30 '20 at 14:42
  • tried but giving error – roy Mar 30 '20 at 14:43
  • 2
    @roy Please describe _in what way_ it was not working. What was the result of that attempt and how did it differ from your desired result? – Patrick Q Mar 30 '20 at 14:43
  • @PatrickQ Or in the loop just `$user_id[]=$row['user_id'];` – AbraCadaver Mar 30 '20 at 14:43
  • @roy Also, "giving error" is not descriptive. Remember, we aren't at your computer. We have no idea what the background of this situation is. We only know _exactly_ what you show/tell us, and nothing more. Please be as detailed as possible. Help us help you. – Patrick Q Mar 30 '20 at 14:46
  • my current code giving me result like 123456 but i need 1,2,3,4,5,6 – roy Mar 30 '20 at 14:47
  • @roy Please update your question to show the exact code that you used in your "I tried like this" attempt, and also tell us what the result was. – Patrick Q Mar 30 '20 at 14:48
  • Does this answer your question? [PHP Add comma to every item but last one](https://stackoverflow.com/questions/5440537/php-add-comma-to-every-item-but-last-one) – Patrick Q Mar 30 '20 at 14:50
  • See also https://stackoverflow.com/questions/4702563/php-for-loop-adding-commas and https://stackoverflow.com/questions/18701079/build-comma-separated-string-in-php-loop – Patrick Q Mar 30 '20 at 14:51

1 Answers1

3

Simply concatenate a comma onto the id, each time you add to the $tmp variable.

$stmt = $con->query($sql);  
$tmp = '';

foreach ($stmt as $row){
  $tmp .= $row['user_id'] . ','; 
}
$tmp = trim($tmp, ',');    // remove trailing comma
echo $tmp;

In answer to your comment:

$v=trim($tmp, ','); 
$insert = $con->prepare("INSERT INTO $dtBe
                        (user_mobile,user_id,posting_date) 
                VALUES ('$umobile',$v,'$posting_date')"); 
$insert->execute();

But there is no benefit or security in preparing a query when you have already concatentated values into.

So use a proper prepared and bound approach.

$v=trim($tmp, ','); 
$insert = $con->prepare("INSERT INTO $dtBe
                        (user_mobile,user_id,posting_date) 
                VALUES (?,?,?)"); 
$insert->bind_param('sss', $umobile, $v,$posting_date);
$insert->execute();
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • hi it is outputting the result as comma separated but now I am getting the error while inserting the comma separated value , below is my insert statement which was working earlier without this comma separated value `$v=trim($tmp, ','); $insert = $con->prepare("INSERT INTO $dtBe(user_mobile,user_id,posting_date) VALUES ('$umobile',$v,'$posting_date')"); $insert2->execute();` it is giving error `call to a member function on boolean` – roy Mar 30 '20 at 15:09
  • Issue is you need to wrap `$v` in quotes as it is a string now there are commas in it so `'$v'` – RiggsFolly Mar 30 '20 at 15:17
  • 1
    But if you are preparing that query, and you should be, then you should also be binding the values to `?` parameters as well. There is no safety in preparing a query that you have already concatenated dangerous values into – RiggsFolly Mar 30 '20 at 15:19
  • as i wrap it with commas '$v' it is saving only first value but as i echo it is showing all the values with comma separated – roy Mar 30 '20 at 15:22
  • Are you sure that `user_id` is the correct column? Sounds like that column may be defined as an integer!! If that is the case you cannot use comma delimited list in there! So you would have to amend the data type – RiggsFolly Mar 30 '20 at 15:25
  • 1
    @roy Are you intending to insert a _list_ of ids into the `user_id` column or a _single_ id? It is not clear. I think you should really update your question (or delete and post a new one) to better describe what your ultimate goal is here. Most likely, your approach as a whole is wrong (or at least not optimal) and getting a better picture of what's going on could help us suggest a better way to achieve your goal. – Patrick Q Mar 30 '20 at 15:26
  • @PatrickQ I think I fell down a rabbit hole question. Thought I knew better :) – RiggsFolly Mar 30 '20 at 15:28
  • oh! how can i thank you, i changed the data type , now it is working like a charm – roy Mar 30 '20 at 15:31
  • 1
    ___It is almost never a good idea to put comma delimited lists of `id`s into a rows column.___ It makes using that information very difficult later in raw SQL – RiggsFolly Mar 30 '20 at 15:35
  • @roy Please know that storing a list (of anything) in a single column is _rarely_ a good idea. – Patrick Q Mar 30 '20 at 15:35
  • just want to know inserting like above or a separate row insert for each user_id which one will be faster considering 20 to 30 records per insert as my all the information is same except user_id's – roy Mar 30 '20 at 15:55
  • Not totally sure what you are doing, but it sounds like a link table might be the way to go – RiggsFolly Mar 30 '20 at 15:57