grozanc Posted September 8, 2013 Share Posted September 8, 2013 (edited) 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 September 8, 2013 by grozanc Quote Link to comment Share on other sites More sharing options...
Solution fastsol Posted September 8, 2013 Solution Share Posted September 8, 2013 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"; Quote Link to comment Share on other sites More sharing options...
grozanc Posted September 8, 2013 Author Share Posted September 8, 2013 Wow, talk about not thinking things through. I originally tried $string_extracredit = 0; as I used that in other spots and stopped the error but didn't think to use $string_extracredit = ''; Anyway, thanks for the help. You solved the problem! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.