Jump to content

Returning mysql rows into an array (i think)


Prank

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
Link to comment
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!
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.