473,801 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arr ay( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}

echo "<ul>\n";
foreach( $array as $string )
{
echo "<li>$strin g</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 4092
I noticed that Message-ID: <pz************ *******@news.xt ra.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_arr ay( $result ) ){
$string.= "<li>".$arrayTe mp['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_arr ay( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}
echo "<ul>\n"
foreach( $array as $string )
{
echo "<li>$strin g</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_ass oc($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_ass oc($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_ass oc($result))
$array[] = '<li>' . $row['something'] . '<li>';

The each place where you want to print the list:

echo "<ul>\n";
echo implode("\n",$a rray[]) . "\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
28943
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 meaningful table. Mick
1
4184
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: http://f1.pg.briefcase.yahoo.com/bc/agascoig/vwp2?.tok=bccV1CVBYRRfg_eF&.dir=/Guest&.dnm=ComplexDoubleMatrixJava.zip&.src=bc It appears that with gcj and a -f switch I can turn off the array bounds checking. Will a JVM ever be smart enough to know that if the array bound is a
4
7697
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 the count(*) field
4
3119
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 Appearently the PHP interpreter converts the VCAHR value automatically in a float despite the fact that the value is defined as a VCAHR in
1
4461
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 want to select all elements of an array then below all elements of another array and so on in a normal column. this is very urgent if some body can help plz.... infact this operation is reverse to insert values from a column into array. thnx n...
9
1669
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 sure.) I did see the DataGrid control but that seems oriented to DB usage. It's probably much more complex than what I need, but I'm not sure about that!
2
3021
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 to insert these data? Please help me. Thanks for your help in advance. Best,
0
1143
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 to DataGridView to SQL. Is is possible to generically map a XmlNodeType.Element Name to a dbml SQL table column?
0
1498
maxamis4
by: maxamis4 | last post by:
Does anyone know how to create a multi column array?
0
9697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10291
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10260
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10049
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9100
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2956
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.