include file and variable

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I've a problem and can't resolve it.

I've a file with only line is a huge query. to make it  
simple, the query is $query = "select * from xxx where mode = ".$mode

Now, this file is included in an other PHP form. Here is the code:

$mode = 1;
$mode = 2;

the first is OK, but the second isn't ok, it still uses the $mode = 1.

Why ? how to fix it ?

Re: include file and variable

Bob Bedford wrote:
Quoted text here. Click to load it

Hi Bob,

So HERE sits the include?

You can think of an include of the code literally inserted at the point  
of include.

So your include says:

$query = "select * from xxx where mode = ".$mode

I would expect this gives you a NOTICE that $mode isn't defined yet.....

Are you sure you have errorreporting configured allright??

Quoted text here. Click to load it

This doesn't change the $query itself.

Quoted text here. Click to load it

I doubt the first was OK.
I think it fetches results for $mode=0, AND produces a notice.

Quoted text here. Click to load it

If you need to put that query into an external file, I would doe it like  
In external file:
$rawquery = "SELECT * FROM xxx WHERE ($mode=**MODE**);";

And then if you need a query, replace **MODE** with the string you  
actually need.

$realquery = str_replace("**MODE**",$mode,$rawquery);
and then use the $realquery.

You could also use prepared statements. It is a little bit more complex,  
but I think it suits your needs.

In general: If you do not know what goes wrong, simply spit out the  
query before executing, so you can see what you are doing.

Hope that helps.

Erwin Moller

Quoted text here. Click to load it

Re: include file and variable

Bob Bedford wrote:
Quoted text here. Click to load it


I'm not clear - are you actually including the file where you have the  
mysql_query() statement in your code?  If so, please post the real code  
you're using - pseudo-code seldom finds problems.

Also, I wouldn't do it like this.  I'd place the query in a function,  
and call the function, i.e.

function doQuery($m) {
   $result = mysql_query("SELECT * FROM xxx... WHERE mode=$m");
   return $result;
// or fetch the data and return it - whatever you wish

Alternatively, you could define the string in the include file such as:

$query = "SELECT * FROM xxx... WHERE mode=";

Then later say:

   $result = mysql_query($query . $mode);

But this can cause other problems because it places a code dependency on  
data external to the module.  For instance, what if someone else defines  
a variable $query?  Or if you need to change the query itself (say add  
another WHERE condition), how many places in your code would have to change?

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: include file and variable

Bob Bedford wrote:
Quoted text here. Click to load it

$query will have the value of $mode as it was when you first included the file.
Changing $mode later won't change what's in $query.

The easiest way to do what you want is to use sprintf()

$query = "select * from xxx where mode = %d";

$mode = 1;

$mode = 2;

Matt M.

Site Timeline