joesaddigh Posted December 6, 2010 Share Posted December 6, 2010 Hi, I have some code which works but when I created a function and call this same code it doesn't. The error I get is as follows: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/fhlinux010/l/languageschoolsuk.com/user/htdocs/admin/email.php on line 42 Error retrieving schools The code function CreateSchoolCheckboxes() { echo '<div style="height:400px;width:400px;font:16px/26px Georgia, Garamond, Serif;overflow:scroll;">'; $querySchools = "SELECT * FROM school"; $result = mysql_query($querySchools, $conn) or die ("Error retrieving schools ".mysql_error()); while($row = mysql_fetch_array($result)) { $schoolname = $row['name']; echo '<input type="checkbox" name="school" value="'.$schoolname.'">'; echo $schoolname . '<br>'; } echo '</div>'; } Im sure that this is probably something simple but any suggestions would be much appreciated. Thanks, Joe Link to comment https://forums.phpfreaks.com/topic/220809-code-works-untill-it-is-in-a-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2010 Share Posted December 6, 2010 You need to pass $conn into the function when you call it - CreateSchoolCheckboxes($conn); Link to comment https://forums.phpfreaks.com/topic/220809-code-works-untill-it-is-in-a-function/#findComment-1143538 Share on other sites More sharing options...
joesaddigh Posted December 6, 2010 Author Share Posted December 6, 2010 Excellent Thanks! I thought that the $conn would still be in scope? Joe Link to comment https://forums.phpfreaks.com/topic/220809-code-works-untill-it-is-in-a-function/#findComment-1143542 Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2010 Share Posted December 6, 2010 Functions have their own private variable scope so that they don't have any unintended interaction with the program where you use the function. Writing larger programs would become impossible if you needed to keep track of all the variables you used in and out of functions. Link to comment https://forums.phpfreaks.com/topic/220809-code-works-untill-it-is-in-a-function/#findComment-1143544 Share on other sites More sharing options...
joesaddigh Posted December 6, 2010 Author Share Posted December 6, 2010 That makes sense I can see the good reason for this. Cheers Link to comment https://forums.phpfreaks.com/topic/220809-code-works-untill-it-is-in-a-function/#findComment-1143546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.