By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

MySQL, turn column into array

P: n/a
Today is the first time I've used MySQL with PHP.

What is the best way to turn a column into an array?

This is how I am doing it at the moment, surely this is sub-optimal:

$query = 'SELECT something FROM table';

$result = mysql_query( $query );

$i = 0;
while( $arrayTemp = mysql_fetch_array( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}

echo "<ul>\n";
foreach( $array as $string )
{
echo "<li>$string</li>\n";
}

echo "</ul>\n";

--
"Come to think of it, there are already a million monkeys on a million
typewriters, and the Usenet is NOTHING like Shakespeare!" - Blair Houghton
-=-=-=-=-=-=-=-=-=-=-=-
http://www.nrkn.com/
-=-=-=-=-=-=-=-=-=-=-=-
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I noticed that Message-ID: <pz*******************@news.xtra.co.nz> from
Nik Coughin contained the following:
Today is the first time I've used MySQL with PHP.

What is the best way to turn a column into an array?

Depends why you want to. How about this (untested)?

$query = 'SELECT something FROM table';
$result = mysql_query( $query );
$string="<ul>\n";
while( $arrayTemp = mysql_fetch_array( $result ) ){
$string.= "<li>".$arrayTemp['something']."</li>\n";
}
$string.= "</ul>\n";

//do other stuff if you want to

echo $string;

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

P: n/a
Nik Coughin wrote (in part):
Today is the first time I've used MySQL with PHP.

What is the best way to turn a column into an array?

This is how I am doing it at the moment, surely this is sub-optimal:

$query = 'SELECT something FROM table';
$result = mysql_query( $query );
$i = 0;
while( $arrayTemp = mysql_fetch_array( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}
echo "<ul>\n"
foreach( $array as $string )
{
echo "<li>$string</li>\n";
}
echo "</ul>\n";


Think about whether you're going to use the information you've
collected more than once. If you are, use an array to store it, if not
use it as soon as you retrieve it.

If you want to store it in an array, you can simplify your code a
little. You don't need the variable $i. Your "while" statement can be
reduced to:

$array = array(); // initialize your array
while ($row = mysql_fetch_assoc($result))
$array[] = $row['something']; // This is much clearer, especially
if you
// decide to fetch more than one
column.

In your case, since it looks like you're creating a list, you could do
it like this:

echo "<ul>\n";
while ($row = mysql_fetch_assoc($result))
echo '<li>' . $row['something'] . "<li>\n";
echo "<ul>\n";

Or if you plan to using the generated list in a number of places:
while ($row = mysql_fetch_assoc($result))
$array[] = '<li>' . $row['something'] . '<li>';

The each place where you want to print the list:

echo "<ul>\n";
echo implode("\n",$array[]) . "\n";
echo "</ul>\n";

Ken

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.