Jump to content

Help with Notice: Undefined variable error


grozanc
Go to solution Solved by fastsol,

Recommended Posts

Hello Everyone,

 

I've got the following snippet of code that is use to retrieve student's grades when they log into the class website. I've been using the bit of code for years, but I'm switching to a new CMS and for the first time I get the "Notice: Undefined variable error". 

 

After a lot of searching I found a solution that I can't figure out how to implement because my variables are coming from an array and not a single instance. Can anyone show me how I should use isset() or empty() to fix the undeclared variable?

 

The undeclared variable error in the following code is $string_extracredit and the error is appearing on line 5.

 

Thanks,

Gary

$query_extracredit  = "SELECT title, points FROM $section WHERE oasis_id='$oasis_id' && extracredit='1' ORDER BY date ASC";
    $result_extracredit = mysql_query($query_extracredit) or die('Error, query failed');
     if (mysql_num_rows($result_extracredit) > 0) { 
	 while(list($extracredit_title, $extracredit_points) = mysql_fetch_array($result_extracredit))
     $string_extracredit .= "".$extracredit_title.": ".$extracredit_points." Points<br/>";
     }
echo "$string_extracredit";
Edited by grozanc
Link to comment
Share on other sites

  • Solution

It's usually best to declare a var that you will be adding onto before you try to add on to it.  The error is coming because the script sees that you are trying to add on to the var using the .= way but you haven't previously declared a value for that var.

    $string_extracredit = '';    
    $query_extracredit = "SELECT title, points FROM $section WHERE oasis_id='$oasis_id' && extracredit='1' ORDER BY date ASC";
    $result_extracredit = mysql_query($query_extracredit) or die('Error, query failed');
    if (mysql_num_rows($result_extracredit) > 0) {
    while(list($extracredit_title, $extracredit_points) = mysql_fetch_array($result_extracredit))
    $string_extracredit .= "".$extracredit_title.": ".$extracredit_points." Points<br/>";
    }
    echo "$string_extracredit";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.