SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Limiting text characters in SQL result

    I am outputting 2 columns onto a web page, title and description.

    Description can get quite long in some cases, how can I limit the output to say, 25 characters?

    Thanks.

  2. #2
    SitePoint Addict CWebguy's Avatar
    Join Date
    Mar 2009
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if using php:

    $shortenedtext=substr($text,0,25);

    http://us.php.net/substr

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    When producing the results to be displayed on a page, do you think it is best limiting the characters produced in the SQL query or using PHP when actually outputting the data?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    depends

    if the column being retrieved is VARCHAR(255) and you only want to show 25 characters, it doesn't much matter

    but if it's LONGTEXT (e.g. 10 megabytes), then yeah, i would do it in the database and not send the unnecessary parts down the pipe
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •