Just the first 100 characters of a 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
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Just the first 100 characters of a query

Post by VKX »

So I have some text in a mysql database value, but I don't want to display all of it. What's the command to only return the first so many letters from it?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I've never done it right off of my query, but you could try substr()
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Would it be acceptible to retrieve the whole entry and then use PHP to only display the first 200 characters? If so something like this should work:

Code: Select all

$dbEntryTeaser = substr($dbEntry, 0, 100);
echo $dbEntryTeaser;
Last edited by dyonak on Thu Oct 06, 2005 4:23 pm, edited 1 time in total.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

double postage 8O
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

but dissimilar enough in that mine suggested using the mysql function substr() and yours the php function...as I said though, I've never used the mysql version...and not sure I ever will 8)
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Perfect! Thanks guys.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You CAN do it in your query

From the manual:

Code: Select all

SUBSTRING(str,pos)  , SUBSTRING(str FROM pos) , SUBSTRING(str,pos,len)  , SUBSTRING(str FROM pos FOR len)

mysql> SELECT SUBSTRING('Quadratically',5);
        -> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);
        -> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
        -> 'ratica'        
mysql> SELECT SUBSTRING('Sakila', -3);
        -> 'ila'        
mysql> SELECT SUBSTRING('Sakila', -5, 3);
        -> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
        -> 'ki'
SUBSTR() is a synonym for SUBSTRING(), added in MySQL 4.1.1.
So if you want the first 100 characters, you could do:

Code: Select all

SELECT
  SUBSTRING(long_field_value,0,100)
FROM
  myTable
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

last I saw, first character was one not zero for SUBSTRING()
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

What if I want it to end on an even word instead of just a letter?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

SUBSTRING_INDEX() or
Useful Posts wrote:Chopping of text without losing words: chopping of text
Post Reply