PHP mySQL request truncates text at a space

Discussion in 'PHP' started by gfreeman, Jun 13, 2007.

  1. #1
    A mySQL table called 'countries' lists the countries in the world, each with an id number.

    This code:

    $sql="SELECT * FROM countries";
    $array=do_sql($sql);
    while ($row = mysql_fetch_array($array, MYSQL_ASSOC)) {
    $n=$row['countryID'];
    $m=$row['countryName'];
    $carray[$n]=$m;
    }

    doesn't work that great. For example, $carray[100] contains "El" instead of "El Salvador", $carray[81] contains "Costa" instead of "Costa Rica", and all the other countries with multi-word names come back as just the first word.

    I've checked the db using phpMyAdmin - edited the entries manually incase the space was some weird character, but that hasn't helped. It could be the db, but the structure of the table is "countryName, varchar(30), latin1_swedish_ci". Maybe it's the php ...

    Can anyone shed light on why I'm getting "Trinidad" instead of "Trinidad and Tobago" etc?
     
    gfreeman, Jun 13, 2007 IP
  2. xxxcoder

    xxxcoder Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    edit array
    data structure and programming lesson :D
    
    $coder=count($carray);
    for($xxx=0;$xxx<$coder;$xxx++) :
    if(strlen($carray[$xxx])<strlen($carray[$xxx-1])) { 
    $phoenix=$carray[$xxx-1];
    $carray[$xxx]=$carray[$xxx-1];
    $carray[$xxx-1]=$phoenix;
    } else { 
    endfor;
    }
    endfor;
    
    PHP:
    sample algorithm
    you are do advanced code
     
    xxxcoder, Jun 13, 2007 IP
  3. gfreeman

    gfreeman Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Aha! Though not exactly the solution I need, it prompted me to check the contents of the array and where it sat in the html. Seems the space in the html was the problem.

    Thanks anyway - you helped me fix it.

    Note to self ... be more patient.
     
    gfreeman, Jun 13, 2007 IP