Jump to content


MYSQL Question


6 replies to this topic

#1 N0valyfe

    Young Padawan

  • Members
  • Pip
  • 15 posts

Posted 16 March 2007 - 03:28 PM

I'm using mysql with php and im wondering if its possible to limit the amount of letters it displays, say i take something from a field with the type as 'text' and i want it to only display the first 150 letters from that field is it possible?

#2 bay

    Young Padawan

  • Members
  • Pip
  • 105 posts
  • Gender:Male
  • Location:Chicago, IL USA

Posted 16 March 2007 - 06:22 PM

yep, just query the result as you normally would, and when you are using it, just use substr ();

substr ( $var, 0, 150 );

#3 SecondV

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Kentucky
  • Interests:All things PHP & MySQL :)

Posted 19 March 2007 - 06:21 AM

Or you could have MySQL do it for you.

SELECT SUBSTRING(field, 1, 150) FROM table;


#4 Herbert

    Young Padawan

  • Members
  • Pip
  • 35 posts
  • Location:Under Your Bed!
  • Interests:Computers, Rock Music, Cars, Girls!

Posted 19 March 2007 - 08:09 AM

View Postbay, on Mar 16 2007, 07:22 PM, said:

yep, just query the result as you normally would, and when you are using it, just use substr ();

substr ( $var, 0, 150 );


I needed this too. Thanks. But one question, where does it go??

Hold on lol. OK here is an example:
If i wanted to display names from my db and i used:

$names

Then to limit the name size, i would use:

substr ( $name, 0, 150 );

??????????

#5 _*Creative Insanity_*

  • Guests

Posted 19 March 2007 - 02:55 PM

It goes with the query for the data you are wanting to limit.

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 19 March 2007 - 03:46 PM

View PostHerbert, on Mar 19 2007, 08:09 AM, said:

View Postbay, on Mar 16 2007, 07:22 PM, said:

yep, just query the result as you normally would, and when you are using it, just use substr ();

substr ( $var, 0, 150 );


I needed this too. Thanks. But one question, where does it go??

Hold on lol. OK here is an example:
If i wanted to display names from my db and i used:

$names

Then to limit the name size, i would use:

substr ( $name, 0, 150 );

??????????


If you were to use it like that, it would look like this.

echo substr($name, 0, 150);

If you are using an array, you would have to iterate the array with either a loop, or a function like array_map().

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 22 March 2007 - 01:32 PM

View PostSecondV, on Mar 19 2007, 05:21 AM, said:

Or you could have MySQL do it for you.

SELECT SUBSTRING(field, 1, 150) FROM table;
You could, but having php do it is by far faster than having sql do it. In fact, having mysql use any "functions" slows things down quite a bit.

View PostHerbert, on Mar 19 2007, 07:09 AM, said:

I needed this too. Thanks. But one question, where does it go??

Hold on lol. OK here is an example:
If i wanted to display names from my db and i used:

$names

Then to limit the name size, i would use:

substr ( $name, 0, 150 );

??????????
http://php.net/substr
http://php.net/manua...fetch-array.php

Please try doing a little research. You'll be surprised how many questions have been answered before you even think to ask them.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users