Page 2 of 6

Re: Undefined variable - why am I getting these?

Posted: Fri Jul 19, 2013 5:06 am
by simonmlewis
Got a new error here:

Code: Select all

 .....$count = 0;
            if ($num_rcstock == 1)
              {
                  $count = $count + 1;
.....
[Fri Jul 19 10:58:11 2013] [warn] [client 82.110.149.20] mod_fcgid: stderr: PHP Notice: Undefined variable: count in /var/www/vhosts/site.co.uk/httpdocs/includes/product.inc on line 1518, referer: http://www...................
I am assigning zero to $count, and then adding one to $count on each cycle, yet it's telling me it's undefined???

Re: Undefined variable - why am I getting these?

Posted: Fri Jul 19, 2013 6:39 am
by Celauran
These lines aren't telling us anything, could you post more of the code? Ideally with line numbers?

Re: Undefined variable - why am I getting these?

Posted: Fri Jul 19, 2013 6:48 am
by simonmlewis
They are actually. Though I failed to tell you which line was the error one.

Code: Select all

 $count = $count + 1;
This is what it doesn't like. I added the $count = 0; because it kept saying $count was undefined.
So I am defining it.

What else does it need??

Re: Undefined variable - why am I getting these?

Posted: Sun Jul 21, 2013 11:57 pm
by Christopher
simonmlewis wrote:

Code: Select all

 $count = $count + 1;
This is what it doesn't like. I added the $count = 0; because it kept saying $count was undefined.
So I am defining it.

What else does it need??
You need to actually think about what is going on in your lines of code. What is the value of $count before you add 1 to it?

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 22, 2013 2:07 am
by simonmlewis
Yes I know.
Surely that is doing BOTH tasks?
$count isn't assigned before. But now I am saying $count is $count + 1.
However in the code I have submitted, it shows $count is 0.

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 22, 2013 8:08 am
by simonmlewis

Code: Select all

if ($row->preorder == "yes") { echo "<br/>pre-order only";}
For this, I am getting this error:

[text][Mon Jul 22 14:05:43 2013] [warn] [client 79.78.112.208] mod_fcgid: stderr: PHP Notice: Undefined property: stdClass::$preorder[/text]
Do I have to do:

Code: Select all

if(isset($row->preorder)
{
if ($row->preorder == "yes") { echo "<br/>pre-order only";}
}
For all these as well?

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 22, 2013 10:25 pm
by Christopher
simonmlewis wrote:Yes I know.
Surely that is doing BOTH tasks?
$count isn't assigned before. But now I am saying $count is $count + 1.
And PHP is telling you that it does not know what unassigned+1 is.
simonmlewis wrote:However in the code I have submitted, it shows $count is 0.
The code is cut up, so I can see if the $count=0; code is actually being executed before the assignment.

Re: Undefined variable - why am I getting these?

Posted: Mon Jul 22, 2013 10:27 pm
by Christopher
simonmlewis wrote:[text][Mon Jul 22 14:05:43 2013] [warn] [client 79.78.112.208] mod_fcgid: stderr: PHP Notice: Undefined property: stdClass::$preorder[/text]
Do I have to do:
For all these as well?
Probably not. Where is $row coming from? The error says it is an object of class stdClass. And that it does not have a preorder property. Find the source of $row and find the source of your problem.

Come on Simon. You can find the source of these errors!

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 3:10 am
by simonmlewis

Code: Select all

$result = mysql_query ("SELECT id, code FROM products WHERE id = '$myprod'");
while ($row = mysql_fetch_object($result))
        {
if ($row->preorder != NULL) { echo "Pre order only";}
}
This is basically it, without hundreds of lines of other code, not related to pre order.
So is this incorrect?

$count has right above it:

$count = 0;

I did this when I saw the error, as I thought if it doesn't know what $count is, then I have to tell it that it's a zero, so at least perhaps it knows it's an integer?! And then it adds 1 each time.

I've no idea how this is wrong.

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 3:36 am
by simonmlewis
Think I have sorted the top one - it wasn't extracting those fields from the DB. I don't know if that is the errors, but at least it's now finding the values.

But what about $count?

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 12:42 pm
by Christopher
Have you verified that $count is actually being initialized?

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 12:45 pm
by simonmlewis
Bearing in mind I have $count = 0; set, does that not do it?
If not, then how?

This is what I just read, and I've been doing this a long time. So I'm confused.

Code: Select all

$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 4:28 pm
by Christopher

Code: Select all

 .....$count = 0;
            if ($num_rcstock == 1)
              {
                  $count = $count + 1;
.....
From the code you gave, I can't tell if $count is being initialized or not. It could be in an if() block that is not run.
[Fri Jul 19 10:58:11 2013] [warn] [client 82.110.149.20] mod_fcgid: stderr: PHP Notice: Undefined variable: count in /var/www/vhosts/site.co.uk/httpdocs/includes/product.inc on line 1518, referer: http://www...................
Also, are you positive that the code above is line 1518?

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 4:33 pm
by simonmlewis
What if there is a block?
Its still being set at zero anyway.
It does work as we see results from it.

Re: Undefined variable - why am I getting these?

Posted: Tue Jul 23, 2013 5:13 pm
by Celauran
If it were being set to zero, you wouldn't be getting the undefined notice.