-4

I've been trying to connect to mysql database and it had worked with the following code:

$host = "localhost";
$username = "skander";
$password = "xxxx";
$db = "ishweb_skander";

    $conn = mysqli_connect($host, $username, $password,$db);

    if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
} 

echo "connected successfully";

then I tried introducing a few other components to facilitate further coding:

class Database {

private $host = "localhost";
private $username = "skander";
public $password = "xxxxxx";
private $db = "ishweb_skander";
public $conn;

public function dbConnection()
{

    $conn = mysqli_connect($host, $username, $password,$db);

    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }
    echo "connected successfully";

and upon loading the pertinent website I receive the following:

Warning: mysqli_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /home/ishweb/www/students/projects/skander/dbconfig.php on line 13 Connection failed: Access denied for user ''@'localhost' (using password: NO)

Why ?

Skander Lejmi
  • 43
  • 1
  • 1
  • 4
  • 3
    Just leave your current code alone. At least until you'll be on good terms with basic OOP. **Mysqli is already a class**, and it is written by professionals. Adding a wrapper of your own will only introduce bugs in your code and nothing else. – Your Common Sense Sep 30 '19 at 09:44
  • Regarding the connection though, here is [how to connect with mysqli properly](https://phpdelusions.net/mysqli/mysqli_connect) – Your Common Sense Sep 30 '19 at 09:47

3 Answers3

0

Your variables inside the method are not defined.

<?php 
class Database
{
    private $host;
    private $username;
    private $password;
    private $db;

    public function __construct()
    {
        $this->host = "localhost";
        $this->username = "skander";
        $this->password = "xxxxxx";
        $this->db = "ishweb_skander";
    }

    public function connect()
    {
        $conn = mysqli_connect($this->host, $this->username, $this->password, $this->db);
        if (!$conn) {
           die("Connection failed: " . mysqli_connect_error());
        }
        return $conn;
    }
}
0

I think the issue here is scope. You are setting your class variables and then when you are trying to reference them within your function you are not using $this->host for example but using $host. The $host variable within the function has not been initialized with a value within the function's scope.

I haven't tested the code below but I believe in your case it would be something like:

class Database {

    private $host = "localhost";
    private $username = "skander";
    public $password = "xxxxxx";
    private $db = "ishweb_skander";
    public $conn;

    public function dbConnection()
    {

        $this->conn = mysqli_connect($this->host, $this->username, $this->password,$this->db);

        if (!$this->conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "connected successfully";
    }

}
Flyingearl
  • 659
  • 4
  • 12
0

Since your information is now in a class. You need to call your variables with $this.

mysqli_connect($host, $username, $password,$db);

becomes

mysqli_connect($this->host, $this->username, $this->password,$this->db);
Rentabear
  • 300
  • 2
  • 11