phpretard Posted October 13, 2008 Share Posted October 13, 2008 How can I convert the code below in a funtion. I then need to call it out and see the results. echo"<select name='SiteArea'>"; $resultshape = mysql_query("SELECT * FROM shape"); while($row = mysql_fetch_array($resultshape)) { $id=$row['id']; $name=$row['name']; echo "<option value='$name'>$name</option>"; } echo "<select>"; Any help today? -Anthony Link to comment https://forums.phpfreaks.com/topic/128174-function-help/ Share on other sites More sharing options...
kishan Posted October 13, 2008 Share Posted October 13, 2008 function getResponse() { global $db; echo "<select name='SiteArea'>"; $resultshape = mysql_query("SELECT * FROM shape"); while($row = mysql_fetch_array($resultshape)) { $id=$row['id']; $name=$row['name']; echo "<option value='$name'>$name</option>"; } echo "<select>"; } call like :$res=getResponse(); Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663888 Share on other sites More sharing options...
phpretard Posted October 13, 2008 Author Share Posted October 13, 2008 Thank you...I have much to learn. PS. What is global $db; Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663900 Share on other sites More sharing options...
Andy-H Posted October 13, 2008 Share Posted October 13, 2008 It makes your db connection variable scope global, therefor you don't need to connect to your database in the function. Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663906 Share on other sites More sharing options...
phpretard Posted October 13, 2008 Author Share Posted October 13, 2008 So if my conection was var $con then it would be: global $con ? Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663911 Share on other sites More sharing options...
Andy-H Posted October 13, 2008 Share Posted October 13, 2008 yeh Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663912 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 But, alas, you never want to use global. Pass the connection resource into the function if you need to use it (in this case, you don't need the db connection inside the function anyway, lol). Link to comment https://forums.phpfreaks.com/topic/128174-function-help/#findComment-663919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.