Jump to content

Can you use include to pull in a file containing variables?


Recommended Posts

I have this at the top of all my pages that will pull from my database.

<?php

include 'config.php';
include 'opendb.php';

$query = mysql_query("SELECT * FROM cms");
while($row = mysql_fetch_array($query))
{
include 'variables.php';

}

?>

 

This is the variables.php file

<?php
    $home = nl2br($row['home']);

$field01 = nl2br($row['field01']);
$field02 = nl2br($row['field02']);
$field03 = nl2br($row['field03']);
$field04 = nl2br($row['field04']);
$field05 = nl2br($row['field05']);

$mot = nl2br($row['team']);
$sta = nl2br($row['sta']);
$pf = nl2br($row['form']);
$add = nl2br($row['additionalforms']);
$fi = nl2br($row['financial']);
$loc = nl2br($row['location']);
$con = nl2br($row['contact']);
$ms = nl2br($row['mission']);
$about = nl2br($row['aboutus']);
$dis = nl2br($row['disclaimer']);
$addr = nl2br($row['address']);
$addr02 =  nl2br($row['address02']);

$new = nl2br($row['newuser']);
$newpass = nl2br($row['newpass']);
?>

 

config.php and opendb.php aren't of any concern and work just fine. Any ideas why this would not be working? Am I abusing include?

I'm not sure why you want to do this. If it did work - it'd make things harder for you. And, it's not the best idea to have an include/require in the middle of a loop.

 

Maybe there is a better way to do what I want to do. I want to update my variables in one spot. I want to have one file that I can stick all that stuff in and reference to it. If there is not a way to do it then that is cool, but perhaps there is a way. So I am asking.

Why not just have the following on the included page:

<?php
$query = mysql_query("SELECT * FROM cms");
while($row = mysql_fetch_array($query))
//...

?>

 

As stated, it's very bad practice including files inside a loop. Especially considering each new row you go through, you'll overwrite the variables.

I don't see why you need a loop with that design. Are you expecting more than one row? You shouldn't be, because your only going to get the values of the last row stored in your variables.

 

Anyway, theres probably not too much wrong with your approuch if only for the fact that your poisoning your application namespace with variables.

 

Seems to me the problem lies in your database design. I'm really not sure why you need all those different fields. In fact, if all you are doing is storing key / value pairs it is likely better to store tham that way.

 

eg;

 

database schema:

CREATE TABLE settings (
  id INT NOT NULL AUTO_INCREMENT,
  k VARCHAR(255),
  v VARCHAR(255),
  PRIMARY KEY (id)
);

 

Now your code.

 

<?php

include 'config.php';
include 'opendb.php';

$config = array();

if ($result = mysql_query("SELECT k, v FROM settings")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($query)) {
      $config[$row['k']] = $row['v'];
    }
  }
}

?>

 

Now you have an array ($config) which holds all your settings. Its still not a great solution, but it seem more logical than your approuch.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.