[solved]Undefined variable and Undefined Index etc

Problems with the Windows version of XAMPP, questions, comments, and anything related.

[solved]Undefined variable and Undefined Index etc

Postby mickeymouse » 02. February 2012 01:40

I've been using XAMPP version 1.6 with no problems.
I changed to version 1.7 and with my same code, I'm getting a whole lot of messages like

Undefined variable and Undefined Index
my code is:
$row = mysql_fetch_row($Results)
$netassets[$row[4]] = $netassets[$row[4]] + $row[3];

Other messages are:
Uninitialized string offset
Undefined offset

What's happened?
Thanks
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Sharley » 02. February 2012 02:18

Things have changed on the forum since your last visit so would you be so kind as to add your XAMPP version and Operating System to your profile, as per these instructions and associated information, so we can effect a suitable solution based on that profile info now and in the future:
viewtopic.php?f=16&t=48626
Thanks kindly. :)
User avatar
Sharley
AF Moderator
 
Posts: 3316
Joined: 03. October 2008 05:10
Location: Yeppoon, Australia Time Zone: GMT/UTC+10
Operating System: Win 7 Pro 32bit/XP Pro SP3

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 02. February 2012 14:25

OK, I've updated my profile with XAMPP ver 1.7.7 and Windows 7.
Thanks
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 02. February 2012 19:28

Hi mickeymouse,

please edit your operation system info. Windows 7 can be everything from Windows 7 Embedded Standard Edition 32Bit up to Windows 7 Ultimate 64Bit with SP1. Especially the information about Windows 7 Type and if you use 32 or 64 bit are very important for providing you the correct support for your environment.

After that, full error messages and sourcecodes will make debugging over the internet much easier for us.
Maybe this thread will help you to get rid of some of your messages.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 03. February 2012 12:51

I've updated my profile concerning info on which Windows 7 is on my computer.
(Had to abreviate - not enough space in box.)

All I could find re Windows was that I have Windows 7 Family Premium Edition, Service Pack 1.
It does not give me any info about 32 or 64 bits.

However in my computer info I found: Computer System Type = 64 bits operating system.
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 03. February 2012 17:16

Thank you very much for updating your os-info :)

Now about your problem. Was the information (especially the link) i gave you helpful to solve some of your problems?
Or do you need any more help for this?

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 04. February 2012 00:24

I could certainly use more help.

I've tried this code
if(isset($_SESSION['key']))
{$key=$_SESSION['key'];}
but it didn't change anything. Probably because I don't understand it.
I suppose I should be replacing 'key' with something; but what?

Many thanks!
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 04. February 2012 09:40

mickeymouse wrote:Probably because I don't understand it.
I suppose I should be replacing 'key' with something; but what?

$_SESSION is also just an example and has to be replaced with the array name you use.
key has to be replaced with the key of that array.

But as i said before: Without your sourcecode and the correct and full error message, we can't provide specific help for your error. It all depends on that.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 04. February 2012 19:14

Thanks for the tip re if(isset('key')) {$key = 'key';}
I tried it but it didn't do anything for me - same error msg.
this is what I put in: if(isset($row[4])){$key = $row[4];}

Here is my code:
line110: while ($row = mysql_fetch_row($Results))
line111: {$Bal[$row[4]] = ($Bal[$row[4]] - $row[3]);
line112: $Bal[$row[5]] = ($Bal[$row[5]] + $row[3]);}

The error msgs I get are:
Notice: Uninitialized string offset: 1 ...in line 111
Notice: Undefined index: 2 ... in line 112
Notice: Undefined index: 9 ...in line 111

I'm getting the correct results (as I was getting with version 1.6 of XAMPP)
but I'm getting all these messages (which I wasn't getting with 1.6)


I've also been getting msgs like this
Notice: Undefined variable: Bal ...in line 111
but I got rid of these by defining $Bal=0; at the beginning of my code.

However, is there a way of turning off all this kind of verification?
My coding is correct and these checks & msgs are just a nuisance.

Hoping this gives you the info you need to get me a solution.
Thanks
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 04. February 2012 20:20

Hi mickeymouse,

mickeymouse wrote:if(isset('key')) {$key = 'key';}
I tried it but it didn't do anything for me - same error msg.

Because the way you use it, it is nonsense. isset() checks whether a variable or array key exists or not.
Code: Select all
//correct, checks a variable
if( isset( $variable ) ) {
    // ... do something
}

//correct, checks an array key
if( isset( $array['key'] ) ) {
    // ... do something
}

You are trying to check a string, which should give you a new php error of type parse error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in ...

mickeymouse wrote:Here is my code:
line110: while ($row = mysql_fetch_row($Results))
line111: {$Bal[$row[4]] = ($Bal[$row[4]] - $row[3]);
line112: $Bal[$row[5]] = ($Bal[$row[5]] + $row[3]);}

Your code is weird.

The errors you get are because the $Bal[$row[4]] and $Bal[$row[5]] (the second part of the lines) are not set when you want to use them. Thats way php norices, that the array keys are undefined.
It would be much more helpful if i would now, which data is in $row, at which time they overwrite each other, etc.
The correct way would be to initialise all $Bal[] keys which are used in the while loop. But for this, i and you have to know which data is in $row[4] and $row[5].
Relating to the error messages, they have to be integers, but which task do they have? Why do you add and substitude them?
I have no idea and i am very sure there is a better way to do what you try.

mickeymouse wrote:I'm getting the correct results (as I was getting with version 1.6 of XAMPP)
but I'm getting all these messages (which I wasn't getting with 1.6)

You haven't got these messages with XAMPP 1.6 because they were suppressed. But thats not the recommend way because that way, you will never learn how to make it the right way.

mickeymouse wrote:I've also been getting msgs like this
Notice: Undefined variable: Bal ...in line 111
but I got rid of these by defining $Bal=0; at the beginning of my code.

Thats good. Even better would be to initialise $Bal as an empty array instead of 0. like this:
Code: Select all
$Bal = array();


mickeymouse wrote:However, is there a way of turning off all this kind of verification?
My coding is correct and these checks & msgs are just a nuisance.

No, your code is not correct. But PHP has got a really good failure tolerance. Thats the only reason why your code still works.
Other programming languages act the other way till your code is absolute correct.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 05. February 2012 00:21

Many thanks for you help & patience.

I've defined $Bal=array() so that takes care of one problem.
I also did $row=array()

Now the $row array contains the transaction records (one rcd at a time of course) fetched from the query of my database file.
This is done by the While($row = mysql_fetch_row($Results)) at the start of the loop.
$row[4] & $row[5 ] are of course fields 4 & 5 of this record and they are different (integer) account numbers.
$row[3] contains a dollar amount.
My code is accumulating the dollar amount in array $Bal[account of $row[4]] and $Bal[account of $row[5]].
e.g., rcd 1 = amt 50.75 acct 7 and acct 9. I am subtracting the amount to $Bal[7] and adding it to $Bal[9].
rcd 2 = amt 25.50 acct 8 and acct 7. I am subtracting the amount to $Bal[8] and adding it to $Bal[7].
etc.
Now, as I said, I'm getting the correct results. So it seems PHP recognizes what is in $row[4] & $row[5 ].
But it seems to be insisting on having the index (and I presume that means defining $row[4] & $row[5 ] - msg ref below.
If that is the case, how do we define that?
(the msg: "Undefined index 7...in line x" or "Undefined index 9...in line y" etc. By the way, the index number it shows in the message, like 7 or 9 etc is what is in $row[4] or $row[5 ]).

You say "The correct way would be to initialise all $Bal[] keys which are used in the while loop".
How do we initialise them? I tried $row[3]=0; $row[4 ]=0; $row[5]=0; but it didn't change anything.

Hoping I've given you the necessary info.
Thanks again.
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 05. February 2012 09:52

Hi mickeymouse,

mickeymouse wrote:Many thanks for you help & patience.

you're very welcome :D

mickeymouse wrote:I also did $row=array()

Thats not needed, because $row is only used in the while loop and $row = mysql_fetch_row() takes care about the correct initializing.

mickeymouse wrote:(the msg: "Undefined index 7...in line x" or "Undefined index 9...in line y" etc. By the way, the index number it shows in the message, like 7 or 9 etc is what is in $row[4] or $row[5 ]).

correct. PHP knows what is in $row[4] and $row[5] but it don't knows what $Bal[7] or $Bal[9] is.
If you read your code line for adding the amount:
account Bal[9] is equal account Bal[9] (which is not defined at this time) plus $row[3]

I don't know how many accounts you have in your database and if the count is constant or raising, so a generic solution will be the best for your problem.


Code: Select all
while( $row = mysql_fetch_row( $Results ) ) {
    if( !isset( $Bal[$row[4]] ) ) {
        $Bal[$row[4]] = 0;
    }
    if( !isset( $Bal[$row[5]] ) ) {
        $Bal[$row[5]] = 0;
    }
    $Bal[$row[4]] = ($Bal[$row[4]] - $row[3]);
    $Bal[$row[5]] = ($Bal[$row[5]] + $row[3]);
}


If you had only 10 accounts and their count is constant, then something like this will be successfull too:
Code: Select all
$Bal = array(1 => 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); //$Bal[1] up to $Bal[10] are setted to 0
while( $row = mysql_fetch_row( $Results ) ) {
    $Bal[$row[4]] = ($Bal[$row[4]] - $row[3]);
    $Bal[$row[5]] = ($Bal[$row[5]] + $row[3]);
}


best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Undefined variable and Undefined Index etc

Postby mickeymouse » 12. February 2012 00:22

The number of accounts is not constant and the solution you gave me was perfect.
Now I understand the problem. I thought it was the row[n] that was the problem.
Thank you so much for your help.
mickeymouse
 
Posts: 58
Joined: 08. March 2009 06:10
Location: France & Canada
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: Undefined variable and Undefined Index etc

Postby Altrea » 12. February 2012 00:43

Hi mickeymouse,

mickeymouse wrote:the solution you gave me was perfect.

I'm glad it worked for you and your problem could be solved :)

mickeymouse wrote:Now I understand the problem. I thought it was the row[n] that was the problem.
Thank you so much for your help.

You are very welcome. It is always a good thing to learn something from a problem :D
I wish you all the best.

Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 112 guests