1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Selecting all phone numbers from a MySQL DB

Discussion in 'PHP' started by bobby9101, Dec 18, 2006.

  1. #1
    ok so I have a MySQL table like:
    
    Table: data
    Where: the phone number is a row called "numbers" and the title is a row called "name"
    
    '1', '1-800-88-GATOR', 'Gatorade'
    
    '2', '1-800-555-TELL', 'TellMe'
    
    There are more numbers, but this is just an example
    
    Code (markup):
    From my little php knowledge I made:
    
    $getphones = mysql_query("SELECT numbers FROM data");
    $phonedata = mysql_fetch_array($getphones);
    $phonenumbers = implode(", ", $phonedata);
    
    echo "$phonenumbers";
    
    Code (markup):
    However it doesn't list all the phone numbers.
    What is wrong with my code?
     
    bobby9101, Dec 18, 2006 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You need to loop through the results.

    
    $getphones = mysql_query("SELECT numbers FROM data");
    while($phonedata = mysql_fetch_array($getphones)){
    $phonenumbers = implode(", ", $phonedata);
    
    echo "$phonenumbers";
    
    }
    
    
    PHP:
    If you just need the phone numbers use:
    
    
    $getphones = mysql_query("SELECT numbers FROM data");
    
    while($phonedata = mysql_fetch_array($getphones)){
    
    echo $phonedata['numbers'];
    
    }
    
    PHP:
     
    jestep, Dec 18, 2006 IP
  3. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Yeah you just forgot to loop through the array and print each number.
     
    GeorgeB., Dec 18, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Just a quick addition to say if you need the numbers in a string ;

    $getphones = mysql_query("SELECT numbers FROM data");

    while($phonedata = mysql_fetch_array($getphones)){

    $output .= $phonedata['numbers'] . " ,";
    $out2[] = $phonedata;

    }

    output contains comma seperated string, out2 contains the array of everything.....
     
    krakjoe, Dec 19, 2006 IP
  5. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think the correct code is :

    $sql = "SELECT number FROM data";
    $getphone = mysql_query($sql);
    while($row = mysql_fetch_array($getphone))
    {
    echo $row['numbers'];
    }
     
    weknowtheworld, Dec 20, 2006 IP
  6. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the only way I could get it to work was to use:
    $getnumbers = mysql_query("SELECT numbers FROM data");
    while($row = mysql_fetch_array($getnumbers))
    {
    echo $row['numbers'] . ", ";
    }
    Code (markup):
    However, it places a , after every number, including the last one. I don't want it on the last one, so I want to use implode.

    if i use impode like:
    $getnumbers = mysql_query("SELECT numbers FROM data");
    while($numberdata = mysql_fetch_array($getnumbers)){
    $phonenumbers = implode(", ", $numberdata);
    
    echo "$phonenumbers";
    }
    Code (markup):
    However that echos something like:

    NUMBER1, NUMBER1NUMBER2, NUMBER2NUMBER3, NUMBER3
    Code (markup):
    where NUMBER* = is a phone number
     
    bobby9101, Dec 20, 2006 IP
  7. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK, update:
    if i use:
    while($numberdata = mysql_fetch_array($getnumbers, MYSQL_ASSOC)){
    PHP:
    I get the correct data, but then implode doesn't work???
    I am too new to this to know why implode won't work.
    And how can I make my keys numberic, so that I don't have to call "MYSQL_ASSOC"
     
    bobby9101, Dec 20, 2006 IP