Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: May 2007
Posts: 12
#1: Nov 11 '07
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.  

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Nov 11 '07

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


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.  
Newbie
 
Join Date: May 2007
Posts: 12
#3: Nov 11 '07

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


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?

Quote:

Originally Posted by Atli

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.  

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#4: Nov 11 '07

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


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.  
Reply