By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,051 Members | 1,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,051 IT Pros & Developers. It's quick & easy.

Undefined variables

P: n/a
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\htdocs\dev\webcounter.php on line 73

Notice: Undefined variable: prefix in g:\program files\apache
group\apache\htdocs\dev\dbclass.php on line 33

Notice: Undefined variable: mysql_errono in g:\program files\apache
group\apache\htdocs\dev\dbclass.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($dbclasspath);
$counter; <================= This is one of the lines (73)
$PHP_SELF = $_SERVER['PHP_SELF'];
$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
if (basename($PHP_SELF)=="webcounter.php") {
// we are indexing a different file
$source = $_GET['src'];
$tmp = substr($source, 0, 6);
if ($tmp=='http:/' || $tmp=='https:' || $tmp=='ftp://') {
$location=$source;
$rpage = $extra_ext.$source;
}
else {
$location=$rootpage.$source;
$rpage = $extra_int.$source;
}
webcounter ($ctype,$rpage);
header ("Location: $location");
exit;
}
/****** end of snip *********/

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Will wrote:
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\htdocs\dev\webcounter.php on line 73


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

-Jay

Jul 17 '05 #2

P: n/a
Will wrote:
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


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 :
Jul 17 '05 #3

P: n/a
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_db($this->db);
$this->check_error('Bad Database Name: ' . $this->db);
}

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

Will wrote:
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\htdocs\dev\webcounter.php on line 73

Notice: Undefined variable: prefix in g:\program files\apache
group\apache\htdocs\dev\dbclass.php on line 33

Notice: Undefined variable: mysql_errono in g:\program files\apache
group\apache\htdocs\dev\dbclass.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($dbclasspath);
$counter; <================= This is one of the lines (73)
$PHP_SELF = $_SERVER['PHP_SELF'];
$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
if (basename($PHP_SELF)=="webcounter.php") {
// we are indexing a different file
$source = $_GET['src'];
$tmp = substr($source, 0, 6);
if ($tmp=='http:/' || $tmp=='https:' || $tmp=='ftp://') {
$location=$source;
$rpage = $extra_ext.$source;
}
else {
$location=$rootpage.$source;
$rpage = $extra_int.$source;
}
webcounter ($ctype,$rpage);
header ("Location: $location");
exit;
}
/****** end of snip *********/


Jul 17 '05 #4

P: n/a
Will <bi*******@yahoo.com> wrote in message news:<Fd*****************@fe1.texas.rr.com>...
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.


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.
Jul 17 '05 #5

P: n/a
Will wrote:
I cannot figure out "prefix" though. I am not sure what
he is trying to do here.
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_books"

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

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

class DB { var $prefix; <===== declared here function prep($name = '') {
if($prefix) { <===== error is coming from here
I think this should be

if ($this-prefix) {

$this->db = $this->prefix . '_' . $name;
} else {
$this->db = $name;
}
@mysql_select_db($this->db);
$this->check_error('Bad Database Name: ' . $this->db);
}

(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 :
Jul 17 '05 #6

P: n/a
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:
Will wrote:
I cannot figure out "prefix" though. I am not sure what
he is trying to do here.

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_books"

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

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


var $prefix; <===== declared here


function prep($name = '') {
if($prefix) { <===== error is coming from here

I think this should be

if ($this-prefix) {
$this->db = $this->prefix . '_' . $name;
} else {
$this->db = $name;
}
@mysql_select_db($this->db);
$this->check_error('Bad Database Name: ' . $this->db);
}


(snip)


Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.