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

Alice
  • 701
  • 1
  • 5
  • 17
  • 4
    Suggestion, [**Don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Lawrence Cherone Nov 29 '13 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 http://stackoverflow.com/q/336078/46675 – Mike B Nov 29 '13 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 – Mark Baker Nov 29 '13 at 16:50
  • Depends of what this class is doing. Is there an connection outside? Are all queries inside this class?... – idmean Nov 29 '13 at 16:50
  • @wumm There is Queries outside. Updated my post. – Alice Nov 29 '13 at 17:01

2 Answers2

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.

Jason Heo
  • 9,956
  • 2
  • 36
  • 64
-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

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34
  • 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. – Zarathuztra Nov 29 '13 at 19:34
  • 1
    The problem with this sort of code is that your service's state is implicit. Maybe consider reading http://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? – Benjamin Gruenbaum Nov 29 '13 at 19:35
  • @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. – Zarathuztra Nov 29 '13 at 19:39