Can anyone help with this PHP please?

Can anyone help with this PHP please?

Author
Discussion

TonyRPH

Original Poster:

12,977 posts

169 months

Thursday 26th March 2015
quotequote all
I am trying to read config variables form a database.

I have a table called 'mytable' like this:

id option_name option_variable
1 foldername /home/folder
2 sitename mysite
3 email example@example.com
4 tagline stuff



I want to be able to read these back and assign variables to them.

e.g.

$foldername=$row['foldername'];
$sitename=$row['sitename'];
$email=$row['email'];
$tagline=$row['tagline'];


My php looks like this:


SELECT id, option_name, option_value FROM my_table;
$result = mysql_query($query) or die("Couldn't execute query");
$count = 1 + $s ;
while ($row=mysql_fetch_array($result)) {
$option_name=$row["1"];
$option_value=$row["2"];
echo $option_name, " ---- ", $option_value, "<br />";



Now although this yields results like:
foldername ---- /home/folder
sitename ---- mysite

This is not quite what I want.

I want to be able to assign each pair as a $variable=variablename

How can I do this?

TIA.



TonyRPH

Original Poster:

12,977 posts

169 months

Thursday 26th March 2015
quotequote all
Thanks, I got somewhere but I think I'm in over my head.

This table contains (or at least will contain!) config parameters for a site.

But despite getting everything displaying correctly in a web page with echo statements, once I tried converting the output to usable variables, I hit a brick wall.

The site works when loading variables from a file using an include statement - so I know that part is correct.

I think I need to go and and find out how data is read from a file and used as variables, vs. reading data from a db and using that as variables.


TonyRPH

Original Poster:

12,977 posts

169 months

Thursday 26th March 2015
quotequote all
There's no reason - I'm just trying to emulate loading the configuration parameters for the site from a database instead of a config file.

I would actually want these variables to be global.

ETA: After giving it some thought, I think I need to use a $_POST statement to make the variables available - however I'm not sure on this!

Edited by TonyRPH on Thursday 26th March 22:26

TonyRPH

Original Poster:

12,977 posts

169 months

Friday 27th March 2015
quotequote all
wombleh said:
<snip>
Personally I use constants loaded from a text file for configuration settings with the define setting. They don't need to change and it means they can't be altered if there are any holes/bugs in the code.
This is what I do at the moment, I just thought it would be nice to have some key editable variables (sitename, meta keywords etc.) stored in a db for easy editing by the end user.

wombleh said:
The problem with putting config into the database is that the means of connecting to the database should be part of the configuration, e.g. if you move it onto a different web host and the DB changes. You'd end up with config spread around different places and IMO it's better to have it in one place with a standard method of accessing it.
I had planned to leave the DB config in a text file, as this would generally be a one off configuration (unless the host changes of course).



TonyRPH

Original Poster:

12,977 posts

169 months

Friday 27th March 2015
quotequote all
droopsnoot said:
<snip>

$options = array();
... query
... while loop (I forget the syntax and can't see it while posting reply)
{
$options[$row['option_name']] = $row['option_variable'];
}

That should give you an array that you can access quite easily using a foreach() loop, and will deal with extra options being added.

foreach($options as $optkey => $optvar) {
echo '<tr><td>' . $optkey . '</td><td>' . $optvar . '</td></tr>';
}

<snip>
Thanks that works (in that it displays the expected output) - however the output is not available as individual variables (even in my test page that I run this from).

Using various methods I do seem to get the output displayed in a web page (that seems to be the easy part!!) but storing the query result in variables appears to be my Achilles heel right now.



TonyRPH

Original Poster:

12,977 posts

169 months

Friday 27th March 2015
quotequote all
I already tried var_dump($options); and I get this:

["$galdir="]=> string(10) "galleries/"
["$thumbnail_size="]=> string(3) "150"
["$intermediate_size="]=> string(3) "800"
["$fulldir="]=> string(10) "fullsized/"

I have no idea why I'm seeing the "=> string.." part.

ETA: You're dealing with a hobbyist here (probably not the best thing for me to be doing - writing stuff like this) but I am trying to learn more about it.

Got it working at last.

Thanks for the help all.



Edited by TonyRPH on Friday 27th March 15:22

TonyRPH

Original Poster:

12,977 posts

169 months

Friday 27th March 2015
quotequote all
I have been reading up on sanitising variables etc. (from memory htmlentity? springs to mind).

Also looking at migrating to mysqli as previously suggested.