variable in include

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

    #1

    variable in include

    HI all,
    Weird problem....

    in the file queries.inc.php I've this:
    $query = 'select * from list where content = '.$idcontent;

    I've this code in my page:
    $idcontent = 15;
    require_once("q ueries.inc.php" );

    The query fails: when I do echo the query I've:

    select * from list where content =

    The $idcontent isn't set in the include...why ? how to fix it ?



  • shimmyshack

    #2
    Re: variable in include

    On May 10, 4:33 pm, "Bob Bedford" <b...@bedford.c omwrote:
    HI all,
    Weird problem....
    >
    in the file queries.inc.php I've this:
    $query = 'select * from list where content = '.$idcontent;
    >
    I've this code in my page:
    $idcontent = 15;
    require_once("q ueries.inc.php" );
    >
    The query fails: when I do echo the query I've:
    >
    select * from list where content =
    >
    The $idcontent isn't set in the include...why ? how to fix it ?
    actually use an include_once (rather than require)

    Comment

    • Ciaran

      #3
      Re: variable in include

      On May 10, 4:33 pm, "Bob Bedford" <b...@bedford.c omwrote:
      HI all,
      Weird problem....
      >
      in the file queries.inc.php I've this:
      $query = 'select * from list where content = '.$idcontent;
      >
      I've this code in my page:
      $idcontent = 15;
      require_once("q ueries.inc.php" );
      >
      The query fails: when I do echo the query I've:
      >
      select * from list where content =
      >
      The $idcontent isn't set in the include...why ? how to fix it ?

      Sounds like the var $idcontent is actually null rather than it being
      an include problem. Try echoing that var on the include page and
      running it on its own

      Ciarán

      Comment

      • Ciaran

        #4
        Re: variable in include

        Sounds like the var $idcontent is actually null rather than it being
        an include problem. Try echoing that var on the include page and
        running it on its own
        >
        Ciarán
        Sorry- my mistake I didnt notice you've defined the var in the page
        itself. I do this exact thing on a bunch of pages and it works fine. I
        use a require_once just like you too. Are you running an old version
        of PHP or something maybe?

        sorry i know I'm not being much help but it all looks good to me!

        Comment

        • Bob Bedford

          #5
          Re: variable in include

          Sorry- my mistake I didnt notice you've defined the var in the page
          itself. I do this exact thing on a bunch of pages and it works fine. I
          use a require_once just like you too. Are you running an old version
          of PHP or something maybe?

          Thanks for your reply. I use PHP 4.4.1


          Comment

          • Bob Bedford

            #6
            Re: variable in include


            "David Gillen" <Belial@RedBric k.DCU.IEa écrit dans le message de news:
            slrnf46gpa.4na. Belial@murphy.r edbrick.dcu.ie...
            Bob Bedford said:
            >HI all,
            >Weird problem....
            >>
            >in the file queries.inc.php I've this:
            >$query = 'select * from list where content = '.$idcontent;
            >>
            >I've this code in my page:
            >$idcontent = 15;
            >require_once(" queries.inc.php ");
            >>
            >The query fails: when I do echo the query I've:
            >>
            >select * from list where content =
            >>
            >The $idcontent isn't set in the include...why ? how to fix it ?
            >>
            Is your $query = .. line inside of function in teh queries.inc.php ?
            If so $idcontent is out of scope.
            NO it's not....
            I've also tried to define the idcontent var as global again in the
            queries.inc.php but it doesn't work either.


            Comment

            • SterLo

              #7
              Re: variable in include

              If you define a variable $x and then INCLUDE a file such as
              include_once("p ath"); it should work.

              So if you have:

              $query = 'select * from list where content = '.$idcontent;

              And

              $idcontent = 15;
              require_once("q ueries.inc.php" );

              You need to change require to include.

              so it should be

              $idcontent = 15;
              include_once("q ueries.inc.php" );

              And you can check this by doing something like

              if ( $idcontent 0 ) {

              }

              Comment

              • ZeldorBlat

                #8
                Re: variable in include

                On May 10, 2:59 pm, SterLo <sterling.hamil ...@gmail.comwr ote:
                If you define a variable $x and then INCLUDE a file such as
                include_once("p ath"); it should work.
                >
                So if you have:
                >
                $query = 'select * from list where content = '.$idcontent;
                >
                And
                >
                $idcontent = 15;
                require_once("q ueries.inc.php" );
                >
                You need to change require to include.
                >
                so it should be
                >
                $idcontent = 15;
                include_once("q ueries.inc.php" );
                >
                And you can check this by doing something like
                >
                if ( $idcontent 0 ) {
                >
                }
                Not true -- require and include behave identically in every way except
                for what happens when the file cannot be included for some reason.

                <http://www.php.net/require>

                Comment

                • Rami Elomaa

                  #9
                  Re: variable in include

                  Bob Bedford kirjoitti:
                  HI all,
                  Weird problem....
                  >
                  in the file queries.inc.php I've this:
                  $query = 'select * from list where content = '.$idcontent;
                  >
                  I've this code in my page:
                  $idcontent = 15;
                  require_once("q ueries.inc.php" );
                  >
                  The query fails: when I do echo the query I've:
                  >
                  select * from list where content =
                  >
                  The $idcontent isn't set in the include...why ? how to fix it ?
                  Just as a sanity test:
                  <?php
                  $foo ='bar';
                  require_once('e cho.php');
                  ?>

                  echo.php:
                  <?php echo $foo; ?>

                  Does the simplest possible test case like the one above work? If it
                  works, then the problem is not in including, but somewhere else. If it
                  doesn't, the latest version of php 4 is 4.4.7, you should upgrade to
                  that if there indeed is a problem related to passing variables to
                  included pages, which I highly doubt.

                  --
                  Rami.Elomaa@gma il.com

                  "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                  usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

                  Comment

                  • Roy Kaldung

                    #10
                    Re: variable in include

                    Bob Bedford wrote:
                    HI all,
                    Weird problem....
                    >
                    in the file queries.inc.php I've this:
                    $query = 'select * from list where content = '.$idcontent;
                    >
                    I've this code in my page:
                    $idcontent = 15;
                    require_once("q ueries.inc.php" );
                    >
                    The query fails: when I do echo the query I've:
                    >
                    select * from list where content =
                    >
                    The $idcontent isn't set in the include...why ? how to fix it ?
                    Hi Bob,

                    away from all prior given replies I strongly suggest you to review your
                    programming style. Use a less a possible global variables in your code.
                    Don't be anger with me, I just want to give you food for thought.

                    hth,
                    Roy

                    Comment

                    Working...