Matt Myers
Matt Myers

Reputation: 25

Problems with variables set inside While Loop being undefined outside.

Okay, so i have created a new support ticket system, but in my ticket search page it keeps giving me errors like undefined variable in line 197. the weird thing is that the variable is defined right above it. Please assist me in this here is a link to the code: http://pastebin.com/AMzRLDK4

I'm trying to make it possible for me to view the support tickets that are open and mark them as read or change the status and to reply to them by going to the pm system. I had it working last night but i must have changed something without realizing its effect.

Thanks in advance, Matt.

Upvotes: 0

Views: 1071

Answers (1)

CLo
CLo

Reputation: 3730

It looks like this is the first time you use $Sid or $Sname in your code. They are inside a code block for the while, which means that is the only place they exist. Also, I think you want to use mysql_fetch_assoc(). It'll actually work with the column names, instead of the indexes. (And probably best off to use the newer MySQLi for several reasons)

while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }

Quick Fix:

$Sid = null; //or 0 whichever makes sense for you
$Sname = null; //or ''
while($raw = mysql_fetch_assoc($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }

However, with the LIMIT 1 in the MySQL Query, you could drop the WHILE all together

$raw = mysql_fetch_assoc($ret);
if($raw === false)
{
    //Error Condition
}

$Sid = $raw['id'];
$Sname = $raw['username'];

Upvotes: 2

Related Questions