help.. Is it correct class to connect to db

Discussion in 'PHP' started by Mr.only, Jun 25, 2012.

  1. #1
    
    
    <?
    class mysqlQueryClass {
        var $result,$resultNoRows;
    
        function dbConnect(){
            $dbConnect=mysql_pconnect(_DATA_BASE_HOST_, _DATA_BASE_USER_, _DATA_BASE_BASSWORD_)
            or die(mysql_error());
    
            mysql_select_db(_DATA_BASE_NAME_)
            or die(mysql_error());
    
    
    
            return $dbConnect;
        }
    
        function dbClose($resultOfQuery, $dbConnect){
            mysql_free_result($resultOfQuery)
            or die(mysql_error());
    
            mysql_close($dbConnect)
            or die(mysql_error());
        }
        
        function dbCloseWOF($dbConnect){
            mysql_close($dbConnect)
            or die(mysql_error());
        }
    
        function mysqlQuery($query){
            $dbConnect=$this->dbConnect();
            
            $resultOfQuery=mysql_query($query)
            or die(mysql_error());
    
            $this->result=mysql_fetch_array($resultOfQuery);
    
            $this->resultNoRows=mysql_num_rows($resultOfQuery);
    
            $dbclose=$this->dbClose($resultOfQuery, $dbConnect);
        }
        
        function mysqlQueryWOF($query){
            $dbConnect=$this->dbConnect();
    
            $resultOfQuery=mysql_query($query)
            or die(mysql_error());
    
            $this->result=$resultOfQuery;
    
            $this->resultNoRows=mysql_num_rows($resultOfQuery);
    
            $dbclose=$this->dbCloseWOF($dbConnect);
        }
        
        function mysqlQueryUpdate($query){
            $dbConnect=$this->dbConnect();
    
            $resultOfQuery=mysql_query($query)
            or die(mysql_error());
    
            $dbclose=$this->dbCloseWOF($dbConnect);
        }
    
        function esc($clean)
        {
            if(@version_compare(@phpversion(), "4.3.0") == "-1") 
            {
                $out = @mysql_escape_string(trim($clean));
            }
            else
            {
                $out = @mysql_real_escape_string(trim($clean));
            }
            return $out;
        }
    
    
    }
    
    ?> 
    
    PHP:
    i want use this class to connect to db .. but I'm not sure that it works well or not ..
    my site on shared server .. and someone told me that the mysql_pconnect its not useful ..
    So .. I want your advice
    and tell me does "mysql_free_result" work well on the class ..

    Note .. This class used in many sites and popular scripts ..
     
    Mr.only, Jun 25, 2012 IP
  2. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    No it is'nt. See the PHP manual:
    http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated

    Which means that use of mysql_* functions is highly discouraged. I would recommend you to use PDO. PDO encourages you to build SQL Injection free queries with prepared statements which are significantly faster and less resource consuming.
     
    shubhamjain, Jun 25, 2012 IP
  3. Mr.only

    Mr.only Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanx shubhamjain i searching for PDO class to use..
    can anyone help
     
    Mr.only, Jun 26, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    I don't see a useful purpose in using that class. Personally I would use PDO. If you would like to using the connection from multiple files simply create a SINGLETON instance of the database connection.

    Basic Example of how to connect with PDO:

    
    $db_name = "someDB";
    $db_user = "myuser";
    $db_pass = "pass123";
    
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=" . $db_name, $db_user, $db_pass);
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }
    
    //$dbh is an active connection
    
    $sql = "UPDATE SomeTable SET SomeCol = 'SomeVal' WHERE SomeCol = 'Whatever';";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    
    PHP:
     
    NetStar, Jun 26, 2012 IP