user2948950
user2948950

Reputation: 137

display database variables in file

So I had my variables all hard coded into my page like so:

$base_url = 'something'; 
$logo_url = 'somethingelse'; 

And then I went and put all these into a database for easier updating, Now I need to place them back into the config.php file so my website can use them.

I tried doing the following: (database.php includes all the connection details)

function get_identifiers() {
  require_once 'database.php';
  $result = mysqli_query($con,"SELECT * FROM settings");
  while($row = mysqli_fetch_array($result))
  {
    $identifier = $row['identifier'];
    $value = $row['value'];
            $identifier = $value
  }
  mysqli_close($con);
}

But I got nothing. What can I do?

Upvotes: 0

Views: 47

Answers (2)

Raggamuffin
Raggamuffin

Reputation: 1760

I think its because your making $identifier = $value... If you want to get the name of the identifier as the variable name use $$identifier = $value;

however I too would suggest using either object or array

$config = new stdClass;

while($row = mysqli_fetch_array($result)) {
    $indentifier = $row['identifier'];
    $config->$identifier = $row['value'];
}

Upvotes: 1

NobleUplift
NobleUplift

Reputation: 6005

You should set your identifiers in an array, return it, and then extract it:

require_once 'database.php';

function get_identifiers() {
  $retval = array();

  $result = mysqli_query($con,"SELECT * FROM settings");
  while($row = mysqli_fetch_array($result))
  {
    $identifier = $row['identifier'];
    $value = $row['value'];
    $retval[$identifier] = $value;
  }
  mysqli_close($con);

  return $retval;
}

Then run this on your page:

extract(get_identifiers());

This will change all of your settings into variables, as you want.

Upvotes: 1

Related Questions