473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

MySQL, turn column into array

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
2 4063
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mick White | last post by:
IS it possible in PHP to grab the headers following a mySQL query? SELECT from tablename. I'm looking for a generic PHP function that will include the headings, so that I can display a...
1
by: agascoig | last post by:
I am just starting to learn Java J2RE 5.0. I coded the matrix library below and found that the array accessing is really slow since each access is checked: ...
4
by: Jaak | last post by:
I have this query, it gives the suspected information in myphpadmin: select listid, count(*) from table1 group by listid How do I put this the result into an array in php? I have problems with...
4
by: rass.elma | last post by:
Hi all I fetch the following value from a string (VCAHR(250))colmun in a MySql table: "300000000000000000000000000000000000000000000000000" When I write it out using echo() I get : 3E+50 ...
1
by: artistlikeu | last post by:
i have a column which contains arrays of variable length. for example one array contains 5 elements, another contains 2 elements, another array 10 elements. i have 500 records in this column i...
9
by: active | last post by:
I need a control that displays a grid of rectangles. That is, like a brick wall except the rectangles are lined up. In VB6 I used such a control (may have been called FlexGrid but I'm not...
2
by: djdarpan | last post by:
Hi, Can anyone help me! I have multiple auto generated input fields(e.g. values are 1 to 10) in a form and I want to insert all input fields data in one column of MySQL table. Is there any way...
0
by: camarkco | last post by:
Hi, What is the best way to get an array of the columns of a SQL table encoded in a dbml file in OrcasBeta2? My project is reading XML into SQL tables, I want to go directy XML to SQL, not XML...
0
maxamis4
by: maxamis4 | last post by:
Does anyone know how to create a multi column array?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.