MySQL Lead-In Sentence fragment...





26
Date Submitted Thu. Oct. 5th, 2006 11:49 AM
Revision 1 of 1
Helper Idlemind
Tags mysql | PHP
Comments 2 comments
"Today in the news, MySQL previews..."

As a "preview" of text, this MySQL select will return the first few words out of a text field in a database. It returns entire words (doesn't cut them off in the middle) and follows the last word with an elipse "..."

In the PHP, assign $CharactersToDisplay to a number - the approximate number of characters desired (or you can hard-code it if you want).

For the example, I also use $TheID as the row identity for the record to display.
SELECT CONCAT(LEFT(NameOfTextField,LOCATE(' ',NameOfTextField,$CharactersToDisplay)-1),'...')
  FROM TheTable
 WHERE TheID = $TheID

Brian Stork

Comments

Comments Handy to know
Tue. Dec. 5th, 2006 1:27 AM    Helper brendo
Comments Fix empty field
Thu. Oct. 30th, 2008 12:51 PM    Beginner nostromo

Voting