Can't get mysqli work in class.

Hello everyone.

I am busy creating a custom CMS that also uses a custom template engine.
But i am coding with MySQL but I know i have to change to MySQLI.

Now the problem is that I can’t get it to work the way it is supposed to.

I created a function connect and it looks like this :

/* @function connect : This function will be used to establich a connection with the database. */ public function connect() { // Set up connection details for the site $con = mysql_connect($this->host, $this->username, $this->password); if (!$con) { die("ShadowCMS could not connect to the database: " . mysql_error()); } // Set up the database $db_select = mysql_select_db($this->db, $con); if(!$db_select) { die("ShadowCMS could not select a database: " . mysql_error()); } }

Now when i change it to this :

/*
@function connect : This function will be used to establich a connection with the database.
*/
public function connect() {
// Set up connection details for the site
$con = mysqli_connect($this->host, $this->username, $this->password, $this->db);
if (!$con) {
die("ShadowCMS could not connect to the database: " . mysqli_error());
}
}

It will always give back the error : database not selected.

How can I solve this ?

The error is pretty clear. You forgot to select your database (add the following code).

mysqli_select_db($this->db, $con);
KBRmedia said

The error is pretty clear. You forgot to select your database (add the following code).

mysqli_select_db($this->db, $con);

Still doesn’t work…
now i get this error :

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\projecten\shadowCMS_beta\shadow\classes\shadow.php on line 105

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\projecten\shadowCMS_beta\shadow\classes\shadow.php on line 107
ShadowCMS could not select a database:

What about this one. Just created

class test
{
    protected $host = 'localhost';
    protected $username = 'root';
    protected $password = '';
    protected $db = 'database';
    protected $link = null;

    public function __construct()
    {
        $this->link = mysqli_connect($this->host, $this->username, $this->password, $this->db);
    }

    public function output()
    {
        $result = mysqli_query($this->link, "SELECT * FROM table LIMIT 10");
        return mysqli_num_rows($result); // THIS IS OUTPUT
    }
}

$test = new test();
echo $test->output();

Output will be 10

I will try it out maybe it will work.
I will let you know in a few hours thanks in advance

black32 said
KBRmedia said

The error is pretty clear. You forgot to select your database (add the following code).

mysqli_select_db($this->db, $con);

Still doesn’t work…
now i get this error :

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\projecten\shadowCMS_beta\shadow\classes\shadow.php on line 105

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\projecten\shadowCMS_beta\shadow\classes\shadow.php on line 107
ShadowCMS could not select a database:
I copied your existing code and didn't go through it. $con and $this->db should be switched.
mysqli_select_db($con,$this->db);
black32 said

I will try it out maybe it will work.
I will let you know in a few hours thanks in advance

If you still can’t get it to work, look for a great database abstraction php scripts that uses mysqli. Will make development easier for you.

Thanks abhimanyusharma003 !

Your code worked exactly how it should.
Now I am recoding everything to mysqli haha.
everyone else thanks for the help !

abhimanyusharma003 said

What about this one. Just created

class test
{
    protected $host = 'localhost';
    protected $username = 'root';
    protected $password = '';
    protected $db = 'database';
    protected $link = null;

    public function __construct()
    {
        $this->link = mysqli_connect($this->host, $this->username, $this->password, $this->db);
    }

    public function output()
    {
        $result = mysqli_query($this->link, "SELECT * FROM table LIMIT 10");
        return mysqli_num_rows($result); // THIS IS OUTPUT
    }
}

$test = new test();
echo $test->output();

Output will be 10

one quick question I want to get my vars from a config file.
First I had a class which had this :

var $host;
var $username;
var $password;
var $db;

But now I need to insert the values in the class itself instead of getting them from the config file.

All right you can use this. Just change the constructor

 public function __construct($host,$username,$password,$db)
    {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        
        $this->link = mysqli_connect($this->host, $this->username, $this->password, $this->db);
    }

Provide details when you create object of class

$test = new test('localhost','username','password','db'); // PROVIDE YOUR DETAILS HERE
echo $test->output();

Thank you very much abhimanyusharma003 that just works fine for me :smiley:
This helps me a lot with my custom CMS :slight_smile:

Good to know, Good Luck