473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to create an array of columns from a database result?

12 New Member
Would you please help me in how to get the remaining column values so that I will be able to echo each value in my pages. Right now, my function is set to return just to return one field's value (line 13) but I want to do the same for all the remaining fields. How do I write loopings like foreach ....
Expand|Select|Wrap|Line Numbers
  1. class Setting extends Domain
  2. {
  3.   function __construct()
  4.   {
  5.     parent::__construct( 'Settings', array 'Settings_id', 'webSite', 'Slogan', 'Title'));
  6.   }
  7.   function Sett()
  8.    {
  9.     global $db;
  10.     $SS = $db->query('SELECT * FROM Settings where webSite = "'.$this->domain().'"');
  11.     while ($SS->fetchInto($row, DB_FETCHMODE_ASSOC)) {
  12.       return $row[webSite];
  13.     }
  14.   }
  15. }
  16.  
  17. $Set = new Setting();
  18. echo $Set->Sett($row[webSite]);
  19.  
Nov 11 '07 #1
3 3803
Atli
5,058 Recognized Expert Expert
Hi.

Please post your code inside [code] tags! It is almost impossible to read without them.

Also, please give your threads descriptive titles. They help our other members finding threads they are likely to be able to help with, thus increasing your chances of getting an answer.

All this, and more, can be read in the Posting guidelines, which all members are required to follow.

Thank you :)

As to your question.

Not knowing exactly which database system you are using, or even how your class handles the database results, I will assume you are using MySQL.

Try something like:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT * FROM tbl");
  2. while($row = mysql_fetch_assoc($result)) {
  3.   $data[] = $row
  4. }
  5. return $data;
  6.  
Nov 11 '07 #2
nasse
12 New Member
Hi, thank you for the help very much. When I tried that, I get array output. When I tried this:

[PHP] $data[] = $row;[/PHP]
[PHP] foreach ($row as $data) {return $data;}[/PHP]

I get the value of the first field. So how do I print the values for the remaining fields from this code. Any idea?

Hi.

Please post your code inside [code] tags! It is almost impossible to read without them.

Also, please give your threads descriptive titles. They help our other members finding threads they are likely to be able to help with, thus increasing your chances of getting an answer.

All this, and more, can be read in the Posting guidelines, which all members are required to follow.

Thank you :)

As to your question.

Not knowing exactly which database system you are using, or even how your class handles the database results, I will assume you are using MySQL.

Try something like:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT * FROM tbl");
  2. while($row = mysql_fetch_assoc($result)) {
  3.   $data[] = $row
  4.   return $data;
  5. }
  6.  
Nov 11 '07 #3
Atli
5,058 Recognized Expert Expert
I'm afraid I made a small error in my previous post. I put the return keyword inside the while loop, which will of course only return the first row of data. I've fixed my previous post.

As to your problem, I think you may be misunderstanding how the return keyword works.
Once you call the return keyword, the function is immediately terminated, returning the value you specify.

So, calling return inside any loop will always terminate the loop and it's parent function on it's first run. In your case, this will always leave you with a single field or row, depending on your code.

Consider this code. It will simply print all fields of all rows:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT * FROM tbl");
  2. $rowIndex = 0;
  3. while($row = mysql_fetch_assoc($result)) {
  4.   echo "Row {$rowIndex}: <br />";
  5.   foreach($row as $_key => $_value) {
  6.     echo "  - {$_key} = {$_value}";
  7.   }
  8. }
  9.  
Nov 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Josep | last post by:
Hi, I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find something helpful. I can get the data,...
9
by: Philip D Heady | last post by:
Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use MySQL but at the office here we use Oracle and boy do I have alot to learn. I'm starting to hate it after using MySQL!! ...
9
by: madsgormlarsen | last post by:
Hi I need to test a field(colum) in a SQL database for for NULL values, and have done so in this way. $query = "SELECT j FROM Andersen"; $result = mysql_query($query, $link_id); $query_data...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
2
by: Fredrik Rodin | last post by:
All, I've been looking around for a solution to my problem for a couple of days now. In short, here's my situation: 1. I'm getting a result from a component back as a datatable and I have...
4
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to...
0
by: Michael Seele | last post by:
hi! i need a list of all primary keys in my database! for this reson i build these sql-statement: /SELECT pg_class.relname AS tableName, pg_constraint.conname AS pkName, pg_constraint.conkey AS...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
15
by: harvey | last post by:
How do I make PHP create a database for mysql please? I can see how to make tables and I have read all the documents I can find but I don't understand how to make the database itself. All...
0
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,...
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...
1
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.