Removing Last Character in a String

In PHP, we will be requiring to remove the last Character from a string variable.

For example, consider below query.

select * from employees where employee_id in (1,2,5,7)

Assume that the employee_id values 1,2,5,7 should be dynamically collected from a webpage provided with checkboxes to select some employees from a list of employees.

Using foreach loop we can create this employee_id string. But it will look like as below

You can note the last comma.

Error will be thrown if we use this as it is

select * from employees where employee_id in ($employee_id_string)

In this case we need to remove this last comma.

It can be easily done by below code.


Posted in MySQL, Php Tagged with: ,