0

I've taken over a Joomla project for a client that had some custom code written using the mysql_ commands running on PHP 5.4. I understand these were deprecated in PHP 7 and need help converting the file to run on PHP 7. I have changed the mysql_ commands to mysqli_ commands. I now have these errors displaying:

Notice: Constant _JEXEC already defined in /XXX/mod_admincustom.php on line 10

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /XXXX/dbcontroller.php on line 21

Warning: mysqli_query() expects at least 2 parameters, 1 given in /XXXX/dbcontroller.php on line 25

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /XXXX/dbcontroller.php on line 26

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /XXXX/dbcontroller.php on line 30

What do I need to change? Thanks :-)

class DBController {
    private $host = "localhost";
    private $user = "XXXX";
    private $password = "XXXX";
    private $database = "XXXX";

    function __construct() {
        $conn = $this->connectDB();
        if(!empty($conn)) {
            $this->selectDB($conn);
        }
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password);
        return $conn;
    }

    function selectDB($conn) {
        mysqli_select_db($this->database,$conn);
    }

    function runQuery($query) {
        $result = mysqli_query($query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
    if (!$result) {
    die(mysqli_error());
}   
    if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;   
    }
}
  • 3
    You shouldn't be using any `mysqli` calls in Joomla. There is an entire set of helper methods for database interactions. I have HEAPS of mysql code snippets for you to peruse at [joomla.se] Stack Exchange. Direct link to my fully explained answers: https://joomla.stackexchange.com/search?q=user%3A12352+%5Bmysql%5D _What do I need to change?_ Short answer: Everything. – mickmackusa Mar 25 '20 at 11:44
  • Possible duplicate of: [How to make a mysqli connection and execute a SELECT query in Joomla?](https://joomla.stackexchange.com/q/15624/12352) – mickmackusa Mar 25 '20 at 11:50

0 Answers0