Jump to content


Photo

Returning mysql rows into an array (i think)


  • Please log in to reply
5 replies to this topic

#1 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 03 July 2006 - 06:26 AM

Hi Guys,

I am building a site which is going to utilise a 'settings' table in mysql which will effect the site depending on the value of certain fields.. does that make sense?

So, I have a 3 field table (id, name & value). Then the rows would be (for example; 1, is_feature_turned_on, Y). So then a simple if statement will display or hide that feature. Now, what I want to know is, whats the best way to reference these names and values? I could be stupid and simple write a new query for each row that I want to reference (SELECT * FROM settings WHERE name = is_feature_turned_on) but thats silly. I dont know how else I can easily access these values, so I thought I must be able to put them into an array somehow and access them like so (name['is_feature_turned_on']) but I simply cannot get my head around how to do this. It might be really simple and something I have just not yet considered.

Anyway, I would greatly appreciate any help at all on this.

Thanks in advance,

Christian
The way to a mans stomach is through his heart.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 06:48 AM

do you want to select all the rows where the value = something? or do you want to select all the information in all of the rows and then be able to manipulate that info with php?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 03 July 2006 - 06:59 AM

do you want to select all the rows where the value = something? or do you want to select all the information in all of the rows and then be able to manipulate that info with php?


I dont need to be able to manipulate it as it should be fine in its current state. I just need to select all and then refer to it

Thanks!
The way to a mans stomach is through his heart.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 July 2006 - 07:11 AM

<?php
//connect to sql and selected your db

//select everything from your table
$sql = "select * from settings";
$result = mysql_query($sql);

//loop through each row
while ($list = mysql_fetch_array($result)) {
   //option 1: 
   //if you are just wanting it to display the list one time, simply echo it here
   //example echo'd format:
   echo $list['id'] . " " . $list['name'] . " " . $list['value'] . "<br>";

   //option 2: 
   //if you need to be able to access these results for later, like
   //passing it to another script eventually, or something, make 
   //an array of the results:
   $info[] = $list;
} //end while loop

//examples for accessing $info data:

//dump everything out:
foreach($info as $key => $val) {
   echo $key . " : " . $val . "<br>";
}

//access a specific row:
//echos the name in the 3rd row..remember arrays start at 0 not 1
echo $info[2]['name']; 
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 03 July 2006 - 08:14 AM

Sweet, I'll give that a go, thanks!! :)
The way to a mans stomach is through his heart.

#6 Prank

Prank
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationSydney, AUS

Posted 03 July 2006 - 09:27 AM

Worked a treat, thanks heaps!
The way to a mans stomach is through his heart.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users