SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Location
    Fosnavg, Norway
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to select just the first 200 characters of a field

    I have a table named "news", wher every colums in the "newstext" field are very large. I wonder how to select just the first 200 characters of every field to show on my web page. I tryed this "$result = @mysql_query("SELECT LEFT(newstext,200) FROM news");". It did not work. I am using this code from Kevin Yank's book:

    <?php

    // Request the text of all the jokes
    $result = @mysql_query("SELECT JokeText FROM Jokes");
    if (!$result) {
    echo("<p>Error performing query: " . mysql_error() . "</p>");
    exit();
    }

    // Display the text of each joke in a paragraph
    while ( $row = mysql_fetch_array($result) ) {
    echo("<p>" . $row["JokeText"] . "</p>");
    }

    ?>

    Hoping someone can help.
    Thanks.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    Svj
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strange
    It's working for me (MySql 3.23.49-nt)

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you could post the code you are using (the one where you do a LEFT()).

    I'm guessing though that you when you tried to fetch it using mysql_fetch_array(), you couldn't because you did not alias LEFT(newstext,200).

  4. #4
    SitePoint Member
    Join Date
    Oct 2002
    Location
    Fosnavg, Norway
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you have the code:

    <?php
    $result=@mysql_query("SELECT LEFT(newstext,200) FROM news");
    if(!$result) {
    echo("<p>Error performing query: " . mysql_error() .
    "</p>");
    exit();
    }

    while($row=mysql_fetch_array($result)) {
    echo("<p>" . $row["newstext"] . "</p>");
    }
    ?>


    It works perfektly iy I skip the LEFT command ("SELECT newstext FROM news").

    By the way, I'm using MySQL version 3.23.51

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    Svj
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redemption
    ...you did not alias LEFT(newstext,200).
    redemption was 100% correct

    Change
    $row["newstext"]
    to
    $row[0]

    or

    Change
    SELECT LEFT(newstext,200) FROM news
    to
    SELECT LEFT(newstext,200) AS newstext FROM news

  6. #6
    SitePoint Member
    Join Date
    Oct 2002
    Location
    Fosnavg, Norway
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I get it:

    while($row=mysql_fetch_array($result)) {
    echo("<p>" . $row["LEFT(newstext,200)"] . "</p>");
    }

    It's working perfectly now
    Thanks

  7. #7
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    Svj
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [off topic]
    Fosnavåg, Norway?
    Close to Aalesund? (beautiful city!)
    [/off topic]

  8. #8
    SitePoint Member
    Join Date
    Oct 2002
    Location
    Fosnavg, Norway
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right. Have you been there?

  9. #9
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    Svj
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A long time ago
    Want to go there again


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
  •