Defining variables in a seperate file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Latio

    Defining variables in a seperate file

    I am missing a trick here on how use variables defined in seperate file.

    In script 1, I have proved to myself my class is working OK because I can
    pass in the values and the echo statement outputs the value of localhost.

    <?php
    // Script 1
    // This script works and outputs the name of localhost
    require_once("c lass.MySQL.php" );
    $db = new MySQL("localhos t", "user", "password", "database") ;
    echo "Name of host: ".$db->host;
    ?>

    However if I assign these values in a seperate file called configuration.p hp
    and put the $variable names in the instantiation statement, it doesn't seem
    to work. Can't understand why it doesn't work because I reckon it should
    still be passing in the values of $localhost, $user, $password and $database
    which is exactly as script 1.

    <?php
    /* Script 2
    This script does not seem assign a value to $localhost
    Here is the contents of config.php without the open/close PHP tags.
    $host = "localhost" ;
    $user = "user";
    $password = "password";
    $database = "database"; */

    require_once("c onfiguration.ph p");
    require_once("c lass.MySQL.php" );
    $db = new MySQL($localhos t, $user, $password, $database);
    echo "Name of host: ".$db->host;
    ?>

    Appreciate any comments. I am sure I have overlooked something very obvious.

    Cheers

    Phil



  • Rik

    #2
    Re: Defining variables in a seperate file

    One of two?
    [color=blue]
    > $host = "localhost" ;
    > $db = new MySQL($localhos t.....[/color]
    [color=blue]
    > Here is the contents of config.php
    > require_once("c onfiguration.ph p");[/color]

    Grtz,
    --
    Rik Wasmus


    Comment

    • Rik

      #3
      Re: Defining variables in a seperate file

      Rik wrote:[color=blue]
      > One of two?
      >[color=green]
      >> $host = "localhost" ;
      >> $db = new MySQL($localhos t.....[/color]
      >[color=green]
      >> Here is the contents of config.php
      >> require_once("c onfiguration.ph p");[/color][/color]

      BTW: this is one of the reasons I like to build code with E_NOTICE on: if
      would have given me notive $localhost is undefined...

      Grtz,
      --
      Rik Wasmus


      Comment

      • Geoff Muldoon

        #4
        Re: Defining variables in a seperate file

        phil.latio@f-in-stupid.co.uk says...
        [color=blue]
        > <?php
        > /* Script 2
        > This script does not seem assign a value to $localhost
        > Here is the contents of config.php without the open/close PHP tags.
        > $host = "localhost" ;[/color]

        Maybe because you've called it $host not $localhost ??

        GM

        Comment

        • Phil Latio

          #5
          Re: Defining variables in a seperate file


          "Geoff Muldoon" <geoff.muldoon@ trap.gmail.com> wrote in message
          news:MPG.1edd53 b5f57714e29897e 1@news.readfree news.net...[color=blue]
          > phil.latio@f-in-stupid.co.uk says...
          >[color=green]
          > > <?php
          > > /* Script 2
          > > This script does not seem assign a value to $localhost
          > > Here is the contents of config.php without the open/close PHP tags.
          > > $host = "localhost" ;[/color]
          >
          > Maybe because you've called it $host not $localhost ??
          >
          > GM[/color]

          Yes, typical I've been looking at this for 2 hours and 5 mins after posting
          I spot my obvious mistake.

          Thanks for taking the trouble.

          Cheers

          Phil


          Comment

          • Tim Van Wassenhove

            #6
            Re: Defining variables in a seperate file

            On 2006-05-23, Phil Latio <phil.latio@f-in-stupid.co.uk> wrote:[color=blue]
            >
            > "Geoff Muldoon" <geoff.muldoon@ trap.gmail.com> wrote in message
            > news:MPG.1edd53 b5f57714e29897e 1@news.readfree news.net...[color=green]
            >> phil.latio@f-in-stupid.co.uk says...
            >>[color=darkred]
            >> > <?php
            >> > /* Script 2
            >> > This script does not seem assign a value to $localhost
            >> > Here is the contents of config.php without the open/close PHP tags.
            >> > $host = "localhost" ;[/color]
            >>
            >> Maybe because you've called it $host not $localhost ??
            >>
            >> GM[/color]
            >
            > Yes, typical I've been looking at this for 2 hours and 5 mins after posting
            > I spot my obvious mistake.[/color]

            Just add the following lines to your scripts (or change your php.ini)
            while you're developping: (It really does make you write less mistakes)

            ini_set('error_ reporting', E_ALL);
            ini_set('displa y_errors', TRUE);


            --
            Met vriendelijke groeten,
            Tim Van Wassenhove <http://timvw.madoka.be >

            Comment

            • Phil Latio

              #7
              Re: Defining variables in a seperate file

              > Just add the following lines to your scripts (or change your php.ini)[color=blue]
              > while you're developping: (It really does make you write less mistakes)
              >
              > ini_set('error_ reporting', E_ALL);
              > ini_set('displa y_errors', TRUE);
              >
              >
              > --
              > Met vriendelijke groeten,
              > Tim Van Wassenhove <http://timvw.madoka.be >[/color]

              Thanks. That is a great tip, I just tried it on the file which I was
              originally having problems with and the error message was very useful.

              Cheers

              Phil


              Comment

              Working...