Jump to content

MySQL Connection - Why Shouldn't you do this?


mds1256

Recommended Posts

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>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.