-1

Example:

class SimpleClass{ public function foo() { mysql_open(); //do mysql query here mysql_close(); } public function boo() { mysql_open(); //do mysql query here mysql_close(); } } 

Or is it better to have one mysql_open in the beginning of the class and one in the end?

Thanks.

EDIT: I use mysqli, this is just an example. Should I open and close in each page file instead? Like in index.php, cataegory.php should have one open and close each.

5
  • 4
    Suggestion, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Nov 29, 2013 at 16:48
  • 2
    Don't do that unless you're doing a TON of processing inbetween. There's overhead to opening and closing connections. Sometimes it's beneficial to free the connection if you're going to be looping and processing over results for several minutes. But it's case by case. Extra reading stackoverflow.com/q/336078/46675 Commented Nov 29, 2013 at 16:48
  • Open a connection in a connection object at the beginning of your script; and pass/inject that connection object into all classes that need it so that they can use the already opened connection Commented Nov 29, 2013 at 16:50
  • Depends of what this class is doing. Is there an connection outside? Are all queries inside this class?... Commented Nov 29, 2013 at 16:50
  • @wumm There is Queries outside. Updated my post. Commented Nov 29, 2013 at 17:01

2 Answers 2

0

Yes, It is bad practice. Here are reasons:

  • cost of making connectio is high
  • cannot use transaction during many function is called
  • every instance has it's own connection. It's too bad
  • and so on

Use PDO, or make singleton db class youself.

Sign up to request clarification or add additional context in comments.

Comments

-4

I would recommend wrapping the connection inside of a DAO class that operates as a singleton to manage your connection. In addition to what others have said above regarding prepared statements, and deprecated functions, I'm going to use those same deprecated functions to demonstrate the DAO concept

<?php class MyGreatDAO{ private static $con = null//late static feature in php 5.3 private __construct(){ } public static getInstance(){ if($con === null){ $con = mysql_connect($server,$user,$pass); } return $con; }//untested 

Basically, the idea is to prevent unnecessary data connections for performance reasons, and just persist the same connection throughout execution. You can use this same class to perform other DB operations as well on $con

3 Comments

I mention the fact it's obsolete in the answer. Additionally, what's wrong with singletons? Its practice is used all the time for this very reason.
The problem with this sort of code is that your service's state is implicit. Maybe consider reading misko.hevery.com/2008/08/17/singletons-are-pathological-liars? If I may suggest just passing the object around so it's clear who is using it rather than having a glorified global (singleton) in your code?
@BenjaminGruenbaum, the comments on that article say a lot. He's got a lot of great points, but so don't a lot of other folks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.