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?
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:
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.....
I think the correct code is : $sql = "SELECT number FROM data"; $getphone = mysql_query($sql); while($row = mysql_fetch_array($getphone)) { echo $row['numbers']; }
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
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"