0

This is my db connect class which is working with php5.3 but it not working after i updated the php5.4 and show error that it's expired.

class DB {
   function DB() {
       $this->host = "localhost";
       $this->db = "dbtest";
       $this->user = "root" ;
       $this->pass = "password";
       $this->link = mysql_connect($this->host, $this->user, $this->pass) or die("<br>Could not connect 1: " . mysql_error());
       mysql_select_db($this->db);
   }
   function query($query) {
       $result = mysql_query($query, $this->link) or die ("<br>Could not execute command 1: ".mysql_error());
       return $result;
   }
   function thislink() {
       return $this->link;
   }
   function close() {
       mysql_close($this->link);
   }
}

How to change it into PDO or mysqli so the wamp could use it

SAR
  • 1,765
  • 3
  • 18
  • 42

3 Answers3

2

You can't.

Your class is not a black box, you have for example a public method that returns the MySQL link identifier:

function thislink() {
   return $this->link;
}

If you change that to another database interface, you will run into problems when you call this method as it will not contain what the calling end is expecting.

The same applies to your public query() method:

function query($query) {
   $result = mysql_query($query, $this->link) or die ("<br>Could not execute command 1: ".mysql_error());
   return $result;
}

That returns a mysql resource in case of for example a SELECT statement so if you change that to msyqli or PDO, the calling side will not be able to handle it.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 2
    Good catch with the *calling side*! – Rizier123 Apr 08 '15 at 09:30
  • 2
    @Rizier123 Thanks! I was about to ask you why you were doing the OP's work but then I realized it wouldn't even work. Database *encapsulation* gone wrong :-) – jeroen Apr 08 '15 at 09:33
  • You would need to change everything, ideally by putting all database stuff in your class and keeping it there. So your `query()` method could return results in an array or a boolean value, but no database resources. Then you can use a @Rizier123's answer to see how that would translate to PDO. And note that these are warnings, you would need to fix it, but everything will continue working. – jeroen Apr 08 '15 at 10:05
1
class DB {
   function DB() {
       $this->host = "localhost";
       $this->db = "dbtest";
       $this->user = "root" ;
       $this->pass = "password";
       $this->link = new PDO("mysql:host=$this->host;dbname=$this->db", $this->user, $this->pass);
   }
   function query($query) {
       $result = $this->link->query($query);
       return $result;
   }
   function thislink() {
       return $this->link;
   }
   function close() {
       $this->link = NULL;
   }
}
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
0

Your class should look something like this:

Note that I change the constructor to __construct(), since in PHP 7 the other way what you used will be deprecated. Also I put the variables as arguments in the constructor with default values. I also enabled error mode for your PDO connection, only have it on in testing!, never in production.

<?php

    class DB {

        public $host;
        public $db;
        public $user;
        public $pass;

        private $link;

        public function __construct($host = "localhost", $db = "dbtest", $user = "root", $pass = "password") {
            $this->host = $host;
            $this->db = $db;
            $this->user = $user;
            $this->pass = $pass;

            try {
                $this->link = new PDO("mysql:host={$this->host};dbname={$this->db}", $this->user, $this->pass);
                $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch(PDOExcpetion $e) {
                echo $e->getMessage();
            }

        }

        function query($query) {    
            $stmt = $this->link->prepare($query);
            $stmt->execute();
            return $stmt;
        }

        function thislink() {
            return $this->link;
        }

        function close() {
            $this->link = null;
        }

    }


?>

For more information about PDO see the manual: http://php.net/manual/en/book.pdo.php

You may also want to take a look into prepared statements.

Rizier123
  • 58,877
  • 16
  • 101
  • 156