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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.