Undefined variables

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

    Undefined variables

    I downloaded a webcounter from
    http://www.math.sunysb.edu/~shafikov...ing/webcounter. It seems to
    be working but I have the following being printed to the page:

    Notice: Undefined variable: counter in g:\program files\apache
    group\apache\ht docs\dev\webcou nter.php on line 73

    Notice: Undefined variable: prefix in g:\program files\apache
    group\apache\ht docs\dev\dbclas s.php on line 33

    Notice: Undefined variable: mysql_errono in g:\program files\apache
    group\apache\ht docs\dev\dbclas s.php on line 43

    I thought I didn't even need to declare variables, so why is it complaining?

    Here is a small part of the code. I could include more if that helps.

    /****** snip *********/
    echo 'Begin VAR dump \n';

    include_once($d bclasspath);
    $counter; <============== === This is one of the lines (73)
    $PHP_SELF = $_SERVER['PHP_SELF'];
    $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
    if (basename($PHP_ SELF)=="webcoun ter.php") {
    // we are indexing a different file
    $source = $_GET['src'];
    $tmp = substr($source, 0, 6);
    if ($tmp=='http:/' || $tmp=='https:' || $tmp=='ftp://') {
    $location=$sour ce;
    $rpage = $extra_ext.$sou rce;
    }
    else {
    $location=$root page.$source;
    $rpage = $extra_int.$sou rce;
    }
    webcounter ($ctype,$rpage) ;
    header ("Location: $location");
    exit;
    }
    /****** end of snip *********/

  • Jay Moore

    #2
    Re: Undefined variables

    Will wrote:[color=blue]
    > I downloaded a webcounter from
    > http://www.math.sunysb.edu/~shafikov...ing/webcounter. It seems to
    > be working but I have the following being printed to the page:
    >
    > Notice: Undefined variable: counter in g:\program files\apache
    > group\apache\ht docs\dev\webcou nter.php on line 73[/color]

    I would imagine it's because you didn't *do* anything with $counter.
    Set it equal to something, make it global, whatever.

    -Jay

    Comment

    • Pedro Graca

      #3
      Re: Undefined variables

      Will wrote:[color=blue]
      > I downloaded a webcounter from
      > http://www.math.sunysb.edu/~shafikov...ing/webcounter. It seems to
      > be working but I have the following being printed to the page:
      >
      > Notice: Undefined variable: counter in g:\program files\apache[/color]

      There are LOTS of scripts made and tested with
      error_reporting (E_ALL ^ E_NOTICE);

      which means no notices will be displayed.
      Usually these notices are nothing to worry about (though IMHO they
      should not be there), so you might reconfigure your PHP to the default
      error reporting and ignore them

      or, better, change the scripts so that they never use undefined
      variables.
      --
      USENET would be a better place if everybody read: : mail address :
      http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      http://www.expita.com/nomime.html : to 10K bytes :

      Comment

      • Will

        #4
        Re: Undefined variables

        Jay was right about counter. Once I initialized that to 0 the error
        goes away. For mysql_errono it looks like the guy that wrote this meant
        to use the function mysql_errno() but used it as a variable and
        misspelled it. I cannot figure out "prefix" though. I am not sure what
        he is trying to do here.

        class DB {

        var $server;
        var $uname;
        var $pword;
        var $prefix; <===== declared here
        var $db;
        var $results;
        var $num_rows;
        var $num_fetched;

        function DB ($user = '', $passwd = '') {
        $this->server = 'localhost';
        $this->uname = $user;
        $this->prefix = ''; <===== used here
        $this->pword = $passwd;
        $this->connect();
        }

        function connect() {
        mysql_connect($ this->server,$this->uname,$this->pword);
        }

        function prep($name = '') {
        if($prefix) { <===== error is coming from here
        $this->db = $this->prefix . '_' . $name;
        } else {
        $this->db = $name;
        }
        @mysql_select_d b($this->db);
        $this->check_error('B ad Database Name: ' . $this->db);
        }

        function check_error($pa ram) {
        if( mysql_errno() == 0 ) {
        return;
        } else {
        echo "ERROR: $param";
        exit;
        }
        }

        Will wrote:
        [color=blue]
        > I downloaded a webcounter from
        > http://www.math.sunysb.edu/~shafikov...ing/webcounter. It seems to
        > be working but I have the following being printed to the page:
        >
        > Notice: Undefined variable: counter in g:\program files\apache
        > group\apache\ht docs\dev\webcou nter.php on line 73
        >
        > Notice: Undefined variable: prefix in g:\program files\apache
        > group\apache\ht docs\dev\dbclas s.php on line 33
        >
        > Notice: Undefined variable: mysql_errono in g:\program files\apache
        > group\apache\ht docs\dev\dbclas s.php on line 43
        >
        > I thought I didn't even need to declare variables, so why is it
        > complaining?
        >
        > Here is a small part of the code. I could include more if that helps.
        >
        > /****** snip *********/
        > echo 'Begin VAR dump \n';
        >
        > include_once($d bclasspath);
        > $counter; <============== === This is one of the lines (73)
        > $PHP_SELF = $_SERVER['PHP_SELF'];
        > $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
        > if (basename($PHP_ SELF)=="webcoun ter.php") {
        > // we are indexing a different file
        > $source = $_GET['src'];
        > $tmp = substr($source, 0, 6);
        > if ($tmp=='http:/' || $tmp=='https:' || $tmp=='ftp://') {
        > $location=$sour ce;
        > $rpage = $extra_ext.$sou rce;
        > }
        > else {
        > $location=$root page.$source;
        > $rpage = $extra_int.$sou rce;
        > }
        > webcounter ($ctype,$rpage) ;
        > header ("Location: $location");
        > exit;
        > }
        > /****** end of snip *********/
        >[/color]

        Comment

        • lawrence

          #5
          Re: Undefined variables

          Will <bigvortex@yaho o.com> wrote in message news:<FdM8c.845 1$TY1.5371@fe1. texas.rr.com>.. .[color=blue]
          > Jay was right about counter. Once I initialized that to 0 the error
          > goes away. For mysql_errono it looks like the guy that wrote this meant
          > to use the function mysql_errno() but used it as a variable and
          > misspelled it. I cannot figure out "prefix" though. I am not sure what
          > he is trying to do here.[/color]

          You don't have to initialize variables in PHP, but it is good
          practice. If you have full error reporting on, PHP will tell you that
          you've got undefined variables. Of course, the default level of error
          reporting is less than "ALL" so most of us never see those errors.

          Comment

          • Pedro Graca

            #6
            Re: Undefined variables

            Will wrote:[color=blue]
            > I cannot figure out "prefix" though. I am not sure what
            > he is trying to do here.[/color]

            Seems like it's a way to choose a database based on the "prefix"

            #v+
            <?php
            $DBinstance->prefix = 'site1';
            $DBinstance->prep('books' ); // use database "site1_book s"

            $DBinstance->prefix = 'site2';
            $DBinstance->prep('books' ); // use database "site2_book s"
            ?>
            #v-

            Of course setting the prefix and calling prep() can be in very different
            parts of the script (or site)

            [color=blue]
            > class DB {[/color]
            [color=blue]
            > var $prefix; <===== declared here[/color]
            [color=blue]
            > function prep($name = '') {
            > if($prefix) { <===== error is coming from here[/color]

            I think this should be

            if ($this-prefix) {

            [color=blue]
            > $this->db = $this->prefix . '_' . $name;
            > } else {
            > $this->db = $name;
            > }
            > @mysql_select_d b($this->db);
            > $this->check_error('B ad Database Name: ' . $this->db);
            > }[/color]
            (snip)

            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            • Will

              #7
              Re: Undefined variables

              Using $this->prefix got rid of the error. The script increments the
              counter. I am not too sure about this script now that it seems to have
              these bugs. I guess I will just need to test it good. Thanks for
              everyones help.

              Pedro Graca wrote:[color=blue]
              > Will wrote:
              >[color=green]
              >>I cannot figure out "prefix" though. I am not sure what
              >>he is trying to do here.[/color]
              >
              >
              > Seems like it's a way to choose a database based on the "prefix"
              >
              > #v+
              > <?php
              > $DBinstance->prefix = 'site1';
              > $DBinstance->prep('books' ); // use database "site1_book s"
              >
              > $DBinstance->prefix = 'site2';
              > $DBinstance->prep('books' ); // use database "site2_book s"
              > ?>
              > #v-
              >
              > Of course setting the prefix and calling prep() can be in very different
              > parts of the script (or site)
              >
              >
              >[color=green]
              >>class DB {[/color]
              >
              >[color=green]
              >> var $prefix; <===== declared here[/color]
              >
              >[color=green]
              >> function prep($name = '') {
              >> if($prefix) { <===== error is coming from here[/color]
              >
              >
              > I think this should be
              >
              > if ($this-prefix) {
              >
              >
              >[color=green]
              >> $this->db = $this->prefix . '_' . $name;
              >> } else {
              >> $this->db = $name;
              >> }
              >> @mysql_select_d b($this->db);
              >> $this->check_error('B ad Database Name: ' . $this->db);
              >> }[/color]
              >
              > (snip)
              >[/color]

              Comment

              Working...