Tuesday, December 8, 2009

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.


More Articles...

1 comment:

Najm Abideen said...

Well my friend, you should add '0' at the end of the string, because some time you will come across that the IN string is empty and mysql will trhough error on that, therefore zero will save you as ids are always greater then 0.


Search This Blog