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;
14
  • 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. Commented Mar 30, 2020 at 14:38
  • php.net/manual/en/function.implode.php Commented Mar 30, 2020 at 14:39
  • I tried like this ",".$row['user_id'] but not working Commented Mar 30, 2020 at 14:41
  • @AbraCadaver Would have to be combined with php.net/manual/en/function.array-column.php Commented Mar 30, 2020 at 14:42
  • 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? Commented Mar 30, 2020 at 14:43

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

12 Comments

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
Issue is you need to wrap $v in quotes as it is a string now there are commas in it so '$v'
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
@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.
It is almost never a good idea to put comma delimited lists of ids into a rows column. It makes using that information very difficult later in raw SQL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.