Computing.Net > Forums > Web Development > Mysql to PHP array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Mysql to PHP array

Reply to Message Icon

Name: jb60606
Date: June 16, 2007 at 14:11:01 Pacific
OS: linux
CPU/Ram: dontknow
Product: dontknow
Comment:

I have a list of cities in a mysql table and I need to get
them into a string separated by a minus ("-").

I used to have the cities in a simple text file, and used the
following code to extract them into a string.

$arr = file("data/cities.dat");
sort($arr);
foreach($arr as $k=>$v){
$arr[$k]=trim($v);
}
$cities = implode('-', $arr);

The result would be:

$cities = "Chicago-New York-Cleveland-Seattle-
Memphis";

Any idea how I can get them out of the table, and into a
string like above?

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: June 16, 2007 at 20:13:53 Pacific
Reply:

The page linked below talks about the mysql_fetch_array function. After you run a query to get the data, you'd use mysql_fetch_array to get each row from the returned data. There are some small examples on the page.

Post back if you need help figuring out the SQL or the database stuff.

http://us.php.net/mysql_fetch_array


0

Response Number 2
Name: Michael J (by mjdamato)
Date: June 17, 2007 at 12:13:08 Pacific
Reply:

Well, it would be helpful to know the structure of your table. BUt, assuming the name of the table is "locations" and the field is "city", this should work:

$query = "SELECT city FROM locations GROUP BY city";

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

while ($record = mysql_fetch_assoc($result)) {

$arr[] = $record['city'];

}

$cities = implode('-',$cityAry);

Michael J


0

Response Number 3
Name: jb60606
Date: June 17, 2007 at 15:09:40 Pacific
Reply:

Thanks a lot guys. Your example worked perfectly, Michael.

I knew how to get the data into a MYSQL array, but getting it
into a string from there is what I didn't have a clue on. I'm
very new to both PHP and SQL.

Thanks again.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Mysql to PHP array

cannot connect to mysql with php www.computing.net/answers/webdevel/cannot-connect-to-mysql-with-php/2968.html

Help with mysql and php? www.computing.net/answers/webdevel/help-with-mysql-and-php/4031.html

PHP arrays and MySQL www.computing.net/answers/webdevel/php-arrays-and-mysql-/3684.html