0

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?

Paul Lo
  • 6,032
  • 6
  • 31
  • 36
user2948950
  • 137
  • 1
  • 4
  • 13
  • Turn on some `error_reporting(E_ALL)`, it will tell you what you're doing wrong. – wesside Dec 12 '13 at 16:12
  • 1
    Why are you making $identifier = $value. Also this is missing a ; at the end of the line – LisaK1308 Dec 12 '13 at 16:13
  • 1
    Pull the require statement out of the function, put it at the top of your php file, just for good practice. – Dan Dec 12 '13 at 16:13
  • @Dan: It's a `require_once`, so it shouldn't cause any issues. – gen_Eric Dec 12 '13 at 16:14
  • 1
    Blank page. That's what I got. I added: `ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);` – user2948950 Dec 12 '13 at 16:14
  • What do you mean by "I got nothing"? What are you doing with `$identifier` and `$value`? Also, why are you overwriting those variables for each row? – gen_Eric Dec 12 '13 at 16:15
  • I had it hard coded in my file to begin with. Now I need to repopulate that list using a database query. `$base_url = 'something';` and then in my regular pages I am `echo $base_url;` to echo out the url `something` – user2948950 Dec 12 '13 at 16:16
  • 1
    I think that you are looking for [dynamic variables](http://stackoverflow.com/questions/9257505/dynamic-variable-names-in-php) - `${$identifier} = $value;` – fvu Dec 12 '13 at 16:20

2 Answers2

1

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.

NobleUplift
  • 5,631
  • 8
  • 45
  • 87
  • 1
    Or, you know, have `get_identifiers` return a value. – gen_Eric Dec 12 '13 at 16:18
  • Then would I have to change what I put in my regular pages to echo out the value? Right now I have: `echo $base_url;` – user2948950 Dec 12 '13 at 16:20
  • He's pulling **settings** from his database. Usually, settings are _system-wide_. However, based on his last comment I'll provide another solution. – NobleUplift Dec 12 '13 at 16:20
  • Try it now. This will put all of your identifiers into an array, return then array, and then extract the keys in your array, turning them into variables. – NobleUplift Dec 12 '13 at 16:24
  • Yay! Was it a misspelling? You should consider registering for Stack Overflow so that you have a unique name and can keep building your reputation. – NobleUplift Dec 12 '13 at 16:28
1

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'];
}
RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
  • The OP's big point though was that he needs to use `echo $base_url;`, and does not want to change his code. Still, I love assigning dynamic variables to a stdClass, +1. – NobleUplift Dec 12 '13 at 16:49