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...
Tuesday, December 8, 2009
Removing Last Character in a String
Labels:
PHP
Related Posts
- Difference between creating objects in PHP and Python
- Sample PHP code for using Google Gemini API
- PHP developer Career opportunities suggested by Gemini
- Challenges in Developing and Testing PHP Script for Automatic Resume Submission
- PHP Code to have different number of rows in first page of PDF file generated by FPDF Library
Search This Blog

AI Course | Bundle Offer | Unlocking AI | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | SQL ebook | 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