/    Sign up×
Community /Pin to ProfileBookmark

Reading a configuration row into constants

I have a configuration table in MySQL with a single row of values, and want to create a series of constants with the values in that row, with the attribute serving as the name of the constant.

It is becoming clear to me that the hoops I’m jumping through to do this are probably way too complicated. No doubt this is a common requirement and it seems to me that I’m probably overcomplicating the issue.

Can anyone suggest how to do this efficiently?

Here’s what I’ve been working with, and it’s not happening for me;

[code=php]// — read the field names from the config row
$result = mysql_query(“SELECT * FROM configuration”) or die(__LINE__.” SELECT Error: “.mysql_error());
$numf = @mysql_num_fields($result);
if(!$numf)$numf=0;
echo “$numf”;

// — read the values from the config row
$row = mysql_fetch_array($result);

// — cycle through each one creating a constant with the value
// — fv == field value
// — fn == field name
// We start the index at 1 because we don’t care about field 0 which is the id attribute
for($i=1;$i<$numf;$i++){
$fn = mysql_field_name($result,$i);
$fv = $row[$fn];
define($fn,$fv);
echo “<br /><b>$fn = $fv</b>”;
}[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 12.2010 — I would probably so something like (untested):
[code=php]
$result = mysql_query("yadda yadda yadda");
if($result != false and mysql_num_rows($result))
{
foreach(mysql_fetch_assoc($result) as $key => $val)
{
if(!is_defined($key)
{
define($key, $val);
}
else
{
user_error("'$key' already defined.");
}
}
}
else
{
// whatever you want to do if nothing returned by query.
}
[/code]
Copy linkTweet thisAlerts:
@scaiferwauthorMay 12.2010 — Brilliant, thank you. A couple of minor tweaks and your code worked beautifully.

When I have time, I'll add a second row to that table with text labels for the attributes so that I can automate a configuration editing form for an admin page.

[code=php]// ----------------------------- LOAD CONFIGURATION DATA FROM DATABASE -----------------------------
// each data element in first row is placed in a constant using attribute name as constant name
$result = mysql_query("SELECT * FROM configuration WHERE id = '1'") or die(__LINE__." SELECT Error: ".mysql_error());
if($result != false and mysql_num_rows($result)) {
foreach(mysql_fetch_assoc($result) as $key => $val) {
if(!defined($key)) {
define(strtoupper($key), $val);
} else {
user_error("'$key' already defined.");
}
}
}
// ---------------------------- /load configuration data from database -----------------------------[/code]
Copy linkTweet thisAlerts:
@scaiferwauthorMay 12.2010 — And one more little tweak, using array_slice() to eliminate the first attribute, the primary key, which is not relevant for our purposes.

[code=php]// ----------------------------- LOAD CONFIGURATION DATA FROM DATABASE -----------------------------
// Each data element in first row is placed in a constant using attribute name as constant name
// The array_slice function eliminates the first attribute in the row, 'id', which is the primary
// key and not relevant for our purposes.
$result = mysql_query("SELECT * FROM configuration WHERE id = '1'") or die(__LINE__." SELECT Error: ".mysql_error());
if($result != false and mysql_num_rows($result)) {
foreach(array_slice(mysql_fetch_assoc($result), 1) as $key => $val) {
if(!defined($key)) {
define(strtoupper($key), $val);
} else {
user_error("'$key' already defined.");
}
}
}
// ---------------------------- /load configuration data from database -----------------------------[/code]
×

Success!

Help @scaiferw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...