Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

PHP: put first 20 chars of a db field in a string

New Here ,
Jun 11, 2006 Jun 11, 2006

Copy link to clipboard

Copied

hi guys,

hopefully an easy problem you can help me with,

i have a field in a sql db that holds a users news post (varchar 500 chars long) , i have a box on my frontpage that
shows the latest 5 news posts, so i need to know how to either put the first 20 chars from the db field into a string to
display in this summary box or just how to limit the output in the box itself.

hope that makes sense.

the code below might help, "newspost" holds the data i need:

<?php echo $row_commentsset['newspost']; ?>

thanks

ted.
TOPICS
Server side applications

Views

167
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 11, 2006 Jun 11, 2006

Copy link to clipboard

Copied

LATEST
tedrodgers wrote:
> i have a field in a sql db that holds a users news post (varchar 500 chars
> long) ,

If you're using MySQL, the VARCHAR columns hold a maximum of 255
characters on versions prior to MySQL 5.0. Does your remote server run
MySQL 5? If not, you need to change the column type to TEXT.

i have a box on my frontpage that
> shows the latest 5 news posts, so i need to know how to either put the first
> 20 chars from the db field into a string to
> display in this summary box or just how to limit the output in the box itself.

In your SQL query:

SELECT LEFT(newspost, 20) AS newspost
FROM tableName

That selects the first 20 characters. Sounds like just what you want.

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines