Hi I am trying to convert the following mysql function to mysqli.

Thanks in advance

David

<?php 
    /*-------------------------------------------------
    This class contains generic database functions to 
    view, add, edit and delete all facets of a database
    -------------------------------------------------*/

    require_once ("sitesettings.php");

    class Database extends SiteSettings {
        var $Query;
        var $Connection;

        function Database() {
            $Settings = SiteSettings::SettingValues();

            $Hostname = $Settings['DB-Hostname'];
            $Username = $Settings['DB-Username'];
            $Password = $Settings['DB-Password'];
            $Database = $Settings['DB-Database'];

            $this->Connection = mysql_connect($Hostname, $Username, $Password);
            mysql_select_db($Database);
            register_shutdown_function(array(&$this, "close"));
        }

        function Execute($SQL) {
            $this->Query = $SQL;
            return mysql_query($SQL, $this->Connection);
        }

        function Records($SQL) {
            return mysql_fetch_array($SQL);
        }

        function RecordCount($SQL) {
            return mysql_num_rows($SQL);
        }

        function Close() {
            mysql_close($this->Connection);
        }
    }
?>

Recommended Answers

All 3 Replies

Member Avatar for diafol

Hi I am trying to convert the following mysql function to mysqli.

OK, so what do you want?

You say that you're trying to convert it - where? I can't see where you've placed any mysqli functions.

Is it the case that you want us to do it for you?

Hi diafol, not expected at all. I replaced the code with my original settings to help make it clear what I had started with.(no offence intended)

This was the warning that I received.

Thanks

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/djenning/public_html/test/includes/functions/database.php on line 22
OK
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/djenning/public_html/test/includes/functions/database.php on line 28

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/djenning/public_html/test/includes/functions/database.php on line 36



<?php 
    /*-------------------------------------------------
    This class contains generic database functions to 
    view, add, edit and delete all facets of a database
    -------------------------------------------------*/

    require_once ("sitesettings.php");

    class Database extends SiteSettings {
        var $Query;
        var $Connection;

        function Database() {
            $Settings = SiteSettings::SettingValues();

            $Hostname = $Settings['DB-Hostname'];
            $Username = $Settings['DB-Username'];
            $Password = $Settings['DB-Password'];
            $Database = $Settings['DB-Database'];

            $this->Connection = mysqli_connect($Hostname, $Username, $Password);
            mysqli_select_db($Database);
            register_shutdown_function(array(&$this, "close"));
        }

        function Execute($SQL) {
            $this->Query = $SQL;
            return mysqli_query($SQL, $this->Connection);
        }

        function Records($SQL) {
            return mysqli_fetch_array($SQL);
        }

        function RecordCount($SQL) {
            return mysqli_num_rows($SQL);
        }

        function Close() {
            mysqli_close($this->Connection);
        }
    }
?>
Member Avatar for diafol

no offence intended

None taken. Just couldn't see what was required. :)

Since you're using a class, you may want to use the OOP format rather than the procedural code.

You could probably to with

class Database extends mysqli

That way Database inherits all of the native functions of the mysqli object. Rename some of them if you really must. You can pass settings to the constructor:

public function __construct($dbhost,$dbuser,$dbpass,$dbname)
{
    ...
    $this->connect($dbhost,...);
}

As mysqli used prepared statements, you may want to think about some of the functions you've created. Execute() for example is too close to the the native execute() method, which runs a prepared statement rather than a bog standard query.

Although some mysql functions have comparable mysqli counterparts, the OOP format often looks and acts differently.

It's also worth noting that some UNIX servers do not install the native driver (mysqlnd), so some methods like get_result() may not be available. This vexed me for some time after I uploaded a script which worked perfectly on my Windows machine to a remote server.

Check out the mysqli pages in the php manual.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.