开发者

How to store configuration variables in mysql?

M'y script's variables are currently stored in a php File as an array. I want to store them in mysql database, however I don't if I should store them as rows or columns. If I开发者_JAVA技巧 use columns it would be easier to retrieve them (only one query), but if I have too many variables the page would have to scroll horizontally and I think it would be hard to find data in phpmyAdmin. If I use rows then how would I retrieve all of them using a single query and store them in the $config array?

Any ideas or suggestions?


Create config table, with 2 columns

Name | Value

and select it with

SELECT * FROM config

so it will look like

Name     | Value
offline  | 1
message  | Hello guys! This is my custom message

to get them into $config, use

$result = (mysql_query("SELECT * FROM config"));
while($row = mysql_Fetch_assoc($result){
    $config[$result['Name']] = $result['Value'];
}

that's it!


Depends.. Are the variables user-based? Do you need to search them? One way to store is in a serialized format (string data in a text field) -- this will suffice if you don't need to search the variables. Otherwise, just store one row per (user-)key-value combination.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜