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
$employee_id_string="1,2,5,7,";
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.
$employee_id_string=substr($employee_id_string,0,-1);
More Articles...
Shop at Rajamanickam.com | Birthday Gift Idea? | Hire me for $6 per Hour
Get 3 useful ebooks for Rs 99 in India and $5.99 globally
Get a 75% commission | ChatGPT and and Google Gemini for Beginners (Use Discount code QPT)
Tuesday, December 8, 2009
Search This Blog
Art of Talking to AI | Tech eBook | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
Today's Deals | Timesheet | Products | 3 ebooks for $5.99 / Rs 99 | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
1 comment:
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.
Thanks,
Najm.
Post a Comment