include file and variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    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($qu ery...
    $mode = 2;
    mysql_query($qu ery

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

    Why ? how to fix it ?
    Bob


  • Erwin Moller

    #2
    Re: include file and variable

    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($qu ery...
    This doesn't change the $query itself.
    $mode = 2;
    mysql_query($qu ery
    >
    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
    >
    >

    Comment

    • Jerry Stuckle

      #3
      Re: include file and variable

      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($qu ery...
      $mode = 2;
      mysql_query($qu ery
      >
      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("SE LECT * 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($qu ery . $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.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Matt Madrid

        #4
        Re: include file and variable

        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($qu ery...
        $mode = 2;
        mysql_query($qu ery
        >
        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(spr intf($query,$mo de));


        $mode = 2;
        mysql_query(spr intf($query,$mo de));


        HTH..
        Matt M.

        Comment

        Working...