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 Quote Link to comment 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(); Quote Link to comment 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; Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 13, 2008 Share Posted October 13, 2008 yeh Quote Link to comment 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). 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.