Get data from mysql

Discussion in 'PHP' started by userman, Apr 18, 2011.

  1. #1
    Hi,

    the php code that i use for get the data from mysql is:
    $result = mysql_query("SELECT * FROM `effects`") or die('Query Error');
    $ittem=mysql_fetch_array($result);

    <?=$ittem['desc'];?>
    and the result is: 1word 2word 3word 4word 5word 6word

    but i dont want the 6word, How can i get the data but with 3word only?

    Regards.
     
    userman, Apr 18, 2011 IP
  2. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you describe your problem a bit more? Is your query returning 1 row and each column of that row is the 1word, 2word, 3word, etc ? Or are you looping through the result set and there's 6 rows?

    Also <?php=$ittem['desc'];?> isn't valid PHP.
     
    AdM.as, Apr 18, 2011 IP
  3. userman

    userman Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Hi AdM.as,

    I use in my script <?php=$ittem['desc'];?> for i get the data from mysql (table "effects", column"desc").
    1 field of the column "desc" have the data: 1word 2 word 3word 4word 5word 6word
    and i want display 1word 2word 3word only.

    Do you understand me now?
     
    userman, Apr 18, 2011 IP
  4. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, if the $ittem['desc'] variable is always formatted in the way you posted, you can use the PHP explode() function to pull the first 3 words only and place them into a string:

    $desc_explode = explode(" ", $ittem['desc']);
    $desc_3words = $desc_explode[0] . ' ' . $desc_explode[1] . ' ' . $desc_explode[2];
    PHP:
    The $desc_3words variable is a string containing 1word 2word 3word.
     
    AdM.as, Apr 18, 2011 IP
    userman likes this.
  5. userman

    userman Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    AdM.as great!!!

    this work for me.

    Thanks very much.

    Regards.
     
    userman, Apr 18, 2011 IP
  6. userman

    userman Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    all right!!

    Thanks again ;)
     
    userman, Apr 18, 2011 IP
  7. AdM.as

    AdM.as Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I was actually going to post again about the first bit of code, it's related to getting 50 characters too.

    If you use the first code posted, that's fine and dandy if all of your desc columns are small articles or something. However, if the desc columns in your setup can be very large, with tens of thousands of words, then that method is probably not the best to use as you will be filling some very large arrays on every query - it's a lot of overhead.

    Instead, a better method would probably be looping through the string itself, detecting the spaces, store the position of the 3rd space and then use the substr() function like so:

    $space_pos = 0;
    $j = 0;
    
    for($i = 0; $i<strlen($ittem['desc']); $i++) {
         if($ittem['desc'][$i] == ' ') {
              $j++;
    
              if($j == 3) {
                   $space_pos = $i;
                   break;
              }
         }
    }
    
    $desc_3words = substr($ittem['desc'], 0, $space_pos);
    PHP:
    With that method, you will at most loop through the string only for as many characters are in your first 3 words put together.

    Now if you want to get the first 50 characters, not taking into account words or anything, then that's much easier:

    $desc_50chars = substr($ittem['desc'], 0, 49);
    PHP:
    Hope this helps :)
     
    AdM.as, Apr 18, 2011 IP