By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,826 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,826 IT Pros & Developers. It's quick & easy.

include file and variable

P: n/a
Hi all,

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

I've a include.inc.php 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;
mysql_query($query...
$mode = 2;
mysql_query($query

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

Why ? how to fix it ?
Bob
Aug 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bob Bedford wrote:
Hi all,
Hi Bob,
>
I've a problem and can't resolve it.

I've a include.inc.php 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:
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??
$mode = 1;
mysql_query($query...
This doesn't change the $query itself.
$mode = 2;
mysql_query($query

the first is OK, but the second isn't ok, it still uses the $mode = 1.
I doubt the first was OK.
I think it fetches results for $mode=0, AND produces a notice.
>
Why ? how to fix it ?
If you need to put that query into an external file, I would doe it like
this:
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.

Regards,
Erwin Moller
Bob

Aug 6 '07 #2

P: n/a
Bob Bedford wrote:
Hi all,

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

I've a include.inc.php 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;
mysql_query($query...
$mode = 2;
mysql_query($query

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

Why ? how to fix it ?
Bob

Bob,

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.
js*******@attglobal.net
==================
Aug 6 '07 #3

P: n/a
Bob Bedford wrote:
Hi all,

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

I've a include.inc.php 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;
mysql_query($query...
$mode = 2;
mysql_query($query

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

Why ? how to fix it ?
Bob

$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;
mysql_query(sprintf($query,$mode));
$mode = 2;
mysql_query(sprintf($query,$mode));
HTH..
Matt M.
Aug 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.