Can you return only so many characters from a mysql query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blurredvision
Forum Newbie
Posts: 9
Joined: Fri Apr 04, 2008 5:34 am

Can you return only so many characters from a mysql query?

Post by blurredvision »

I'm getting ready to venture further into PHP territory by attempting to set up a way for users of my site to create a new item on the front page by "linking" to one of their forum posts. The way that I was thinking of setting this up was letting them create their post first, then choosing an option to create an item on the front page. What I would want to happen next is for the first so-many characters from their post be copied into another table that will be called by the main page. But obviously, if they create a very large post, I don't want my entire front page be cluttered with text after text.

For example, let's say someone wants to post their thoughts on the current Democratic primaries, and they insisted on typing up a 3000-word opinion piece. If they wanted to display this on the front page, I would only want to call, say, the first 300 or 400 characters of that post to display, then I would create a link back to their forum post for someone to read further.

I haven't started piecing this together yet, partly due to lack of time and partly due to nervousness of doing something on this scale (this would be a big project for a person of my skill level). So, before I actually start, I think this truncating of their post is the only thing that I'm not quite sure how to do. Can anyone get me started by giving me a function to research, or a couple of lines of code as an example of how to truncate a post that I've called out of mySQL?

I appreciate any advice.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can you return only so many characters from a mysql query?

Post by Christopher »

SQL has functions that will give you only the first part of a text column containing an article. Likewise, if the articles are in files you could read only the first part of the file.
(#10850)
Post Reply