SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2000
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm looking for a way to shorten my MySQL results, to a certain number of characters for each result taken from the database.

    ie:
    I need
    "I was raised by rabid wolves in an eskimo on the outskirts of france"

    shorted to:
    "I was raised by rabid wolves in an es..."

    Is there a quick way to do this kind of thing using MySQL and PHP3?

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this in your query
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    SELECT substring(FIELD, 1, LENGTH) FROM tableName
    [/code]

    Where field is the field which stores the data you want to trim and length is the lenght you want returning.

    OR
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    SELECT substring(FIELD, 1, round(char_length(FIELD) * 0.10) ) FROM tableName
    [/code]

    This would return 10% of the data from the cell called FIELD.

    Hope these help you.

    ------------------
    Karl Austin
    KDA Web Services

  3. #3
    SitePoint Member
    Join Date
    May 2000
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a heap for the code Karl..cant say it helped that much. If you can see something wrong with this code...let me know:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>$shortbio = mysql_query("SELECT substring($biography, 1, 100) FROM artist WHERE artist_id='$id'",$db);[/code]

    The thing i'm trying to do is grab a band biography from the database, and shorten it down to save space. I use
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>&lt;?php printf("%s \n", mysql_result($shortbio,0,"biography")); ?&gt;[/code]
    to try and print the shortent bio to the page, but that doesn't seem to be working. I must be doing something not quite right...or completetly wrong. One of the two

    [This message has been edited by insomniac (edited May 21, 2000).]

  4. #4
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    $sql = "SELECT substring(biography, 1, 100) FROM artist WHERE artist_id='$id'";
    $res = mysql_query($sql, $db);

    $bio = mysql_fetch_row($res); $bio = $bio['0'];
    [/code]

    This code should work for you. $bio will store the short biography for you.

    ------------------
    Karl Austin
    KDA Web Services

  5. #5
    SitePoint Member
    Join Date
    May 2000
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again karl...everything works really well now. I've been going through various PHP3 and MySQL source code for about 5 hours now

    I think I need some more coffee.


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
  •