Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    2,492
    Tokens
    147

    Latest Awards:

    Default [PHP/SQL] First letter of name

    Okay so I have a table with my users in it.

    I have a page that displays all my users sorted alphabetical.

    Now if I wanted to show only the users that have a name that starts with the letter "A or a" how do I do this in php/Sql ?

    +rep

    Thanks,
    Olivier

  2. #2
    Join Date
    Aug 2006
    Location
    Manchester, UK
    Posts
    2,016
    Tokens
    141
    Habbo
    florx

    Latest Awards:

    Default

    PHP Code:
    <?
    $sql 
    mysql_query("SELECT * FROM users ORDER BY username ASC"); 
    while (
    $r mysql_fetch_array($sql)){
    $str $r[name];
    $first $str[0];
    if(
    $first == "A" || $first == "a"){
    echo 
    "$r[name]";
    }
    }
    ?>
    This assumes you have already connected to the database to run the query.
    Last edited by Florx; 11-11-2007 at 10:11 PM.

  3. #3
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    2,492
    Tokens
    147

    Latest Awards:

    Default

    Trough googling I had already found a solution, but i'll fav. ur post so i dont forget for other projects.

    +rep for helping

    ty,
    oli


    Quote Originally Posted by FlorX View Post
    PHP Code:
    <?
    $sql 
    mysql_query("SELECT * FROM users ORDER BY username ASC"); 
    while (
    $r mysql_fetch_array($sql)){
    $str $r[name];
    $first $str[0];
    if(
    $first == "A" || $first == "a"){
    echo 
    "$r[name]";
    }
    }
    ?>
    This assumes you have already connected to the database to run the query.

  4. #4
    Join Date
    Aug 2006
    Location
    Manchester, UK
    Posts
    2,016
    Tokens
    141
    Habbo
    florx

    Latest Awards:

    Default

    Alrighty

    Good luck with the PHP.

  5. #5
    Join Date
    Sep 2006
    Location
    Hobart, Australia
    Posts
    593
    Tokens
    0

    Default

    You can just do this with SQL...

    PHP Code:
    <?
    $sql 
    mysql_query("SELECT * FROM users WHERE username LIKE 'a%' OR LIKE 'A%' ORDER BY username ASC"); 
    while (
    $r mysql_fetch_array($sql)){
    echo 
    $r['username'] . '<br />';
    }
    ?>

  6. #6
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Quote Originally Posted by benzoenator View Post
    You can just do this with SQL...

    PHP Code:
    <?
    $sql 
    mysql_query("SELECT * FROM users WHERE username LIKE 'a%' OR LIKE 'A%' ORDER BY username ASC"); 
    while (
    $r mysql_fetch_array($sql)){
    echo 
    $r['username'] . '<br />';
    }
    ?>
    No that will make any name with the letter A in it will show..

  7. #7
    Join Date
    Jul 2005
    Posts
    1,653
    Tokens
    50

    Latest Awards:

    Default

    @Blob:
    http://dev.mysql.com/doc/refman/5.0/...-matching.html

    Read the first bit on the page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •