We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,608 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

echo row in php

Hi everyone, I need a wee bit of help with the following.

I am calling all country names from a db table and echo-ing out each country name in a list format.

$query = "select * from countries order by Country Desc"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
    echo $row['Country'];
    echo "<br />";
}

this gives me a list of country names from the database, what I would like to do is have the list look like the following.

["country name 1", "country name 2", "country name 3", "country name 4"]

How can I acheive this, thanks

2
Contributors
2
Replies
36 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
DaveyMoyes
Junior Poster
122 posts since May 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0

Sounds like you want json_encode()

$countries = array ();
while($row = mysql_fetch_array($result)){
    $countries[] = $row['Country'];
}
echo json_encode($countries);
pritaeas
Posting Prodigy
Moderator
9,549 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,497
Skill Endorsements: 98

Oh yes, that is perfect, cheers

DaveyMoyes
Junior Poster
122 posts since May 2009
Reputation Points: 9
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0577 seconds using 2.66MB