mds1256 Posted February 7, 2012 Share Posted February 7, 2012 The below scenario works fine but why shouldnt you do this? Why can I not just run the mysql_connect and mysql_select_db at the top of each page and then run my queries under all of this. What I have read is that the mysql_connection will die after the script finishes (at the end of the page) any way so why do people create mysql_connection objects as php is stateless so it doesnt save this any way. <?php mysql_connect('localhost', 'user', 'pass'); mysql_select_db('test'); ?> <html> <body> <?php $query = mysql_query("Select * from data"); while($row = mysql_fetch_array($query)) { echo $row['name']."<br />"; } ?> -------------- OTHER HTML HERE --------------- <?php $query = mysql_query("Select * from addresses"); while($row = mysql_fetch_array($query)) { echo $row['postcode']."<br />"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256594-mysql-connection-why-shouldnt-you-do-this/ Share on other sites More sharing options...
trq Posted February 7, 2012 Share Posted February 7, 2012 There is nothing wrong with that. Most people write php database classes because they don't know what they are doing. Good ones are done well, aren't tied to a particular database server type and provide better functionality in a more useful interface. Quote Link to comment https://forums.phpfreaks.com/topic/256594-mysql-connection-why-shouldnt-you-do-this/#findComment-1315411 Share on other sites More sharing options...
mds1256 Posted February 7, 2012 Author Share Posted February 7, 2012 There is nothing wrong with that. Most people write php database classes because they don't know what they are doing. Good ones are done well, aren't tied to a particular database server type and provide better functionality in a more useful interface. That is great! You are on fire, you have helped me out on a few things over the past few days. Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/256594-mysql-connection-why-shouldnt-you-do-this/#findComment-1315417 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.