Jump to content

Archived

This topic is now archived and is closed to further replies.

Prank

Returning mysql rows into an array (i think)

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote author=Crayon Violent link=topic=99267.msg390886#msg390886 date=1151909295]
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?
[/quote]

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!

Share this post


Link to post
Share on other sites
[code]
<?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'];
?>
[/code]

Share this post


Link to post
Share on other sites
Sweet, I'll give that a go, thanks!! :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.