1

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 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?

5
  • Why would you want to do that? Unless it's user-configuration settings, or changes every couple of days, it does not belong into the database. That's a redundant extra query for each page invocation. Commented Sep 21, 2011 at 15:40
  • So the admin can change things like URL title keywords directly from the admin panel without having to open the php file. Commented Sep 21, 2011 at 15:42
  • Well you can use an .ini file for that. Reading and rewriting them is as simple. Or did you just want to refer them to phpMyAdmin to avoid the coding overhead of an admin panel for a config settings file? (Shameless plug: milki.include-once.org/genericplugins/genconfig.html) Commented Sep 21, 2011 at 15:46
  • Storing them in database would simplify changing some basic variables. Something like the wordpress settings. Commented Sep 21, 2011 at 15:50
  • No difference to any other configuration store. (Except again that WP-style database config tables are inefficient at runtime. That's a widespread but unprofessional malpractice from the phpNuke era.) Commented Sep 21, 2011 at 15:55

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

3 Comments

How would I store them in an array. I need something like this $config["url"] would get something.com
Ok say I use $config["offline"] would that echo 1?
@Kbr: On the left from my answer, there is a tick - click it
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.