I have a database which contains some topics each with a title and some description. In the title page, I have to display all the titles and a few lines of description for that topic. When a user clicks on one topic then he is taken to a page where he can find the entire description. Exactly like stackoverflow site, where description of each question is displayed in short.

I was wondering is there a way we can retrieve such short description from the database directly or should the front end parse the retrieved record and truncate it accordingly? Is there a way to query for a short version of the record from the database directly. I am using MySql as my database and php to display web pages.

Accepted Answer

I would say the answer is found in this post: SQL Server substring breaking on words, not characters

You wouldn't want to just limit by a string length, since then you might cut off a word right in the middle. Instead, you would want to set your max length and then move back to the place where there is a space.

Another option would be to run this once and then update a field in the table that stores the truncated text. That way you could limit your extra SQL calls and yet still have the nicely-formatted summary text.

Written by BiggsTRC
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki