473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

include file and variable

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
3 1520
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: JStrummer | last post by:
I have a question regarding paths and the include() statement in PHP. I develop in a Windows environment and will be publishing to a Linux server. I would like to do the following: 1. Setup my...
2
by: Marcus | last post by:
Hello, I am having problems with an include statement. I'm setting a session variable flag and then including a file, and in that include file I have a check at the top to make sure that the...
2
by: chopper | last post by:
Please excuse the long post but I can't think of shorter way of explaining the issue. Please could someone advise on the following: I'm writing preview functionality for a CMS written in ASP....
11
by: wenmang | last post by:
Hi, all: What will happen if header files with the exact same names but with different contents and declared/defined without include guard reside in the include path for the compiler? Are these...
3
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
6
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file =...
4
by: spectre | last post by:
Hello, I first create a session variable 'client' in the login.asp file The login.asp file opens a file (xxx.asp) with an include file .. If I write <!--#include file="../../../data/" &...
6
by: j.woodcock | last post by:
is there a way of having a file that's name is a variable (eg dependant on the user name) act like a include. i know that you cant define the file for an include asp tag using a variable and that...
4
by: rocketeer | last post by:
I've a set of Javascript classes that maintain state. For example, gm.js might be: var GroupManager { groups: {} }; Over time I add new groups to the list: GroupManager.groups = myGroup; ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.