Hello,

I am trying to connect with the database but the file contains OOP programing that is little bit unfimiliar for me if anyone help me out in connecting with the database.

Db_connect.php

<?php
class DB_Connect {

    var $dbuser;
    var $dbpass;
    var $dbname;
    var $dbhost;
    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once 'include/config.php';
        $this->dbuser = 'rankdroid';
        $this->dbpass = 'droid1234';
        $this->dbname = 'rankdroid';
        $this->dbhost = 'localhost';

        // connecting to mysql
        $this->dbc = mysqli_connect($this->dbuser, $this->dbpass, $this->dbname, $this->dbhost) or die('Error connecting to DB'); 
        //$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        // selecting database
        //mysql_select_db(DB_DATABASE);

        // return database handler
        return $con;
    }

    public function query($sql)
    {
        return mysqli_query($this->dbc, $sql) or or die('Error:'.mysqli_error($this->dbc).', query: '.$sql);
    }

    public function fetch($sql)
    {        
        $array = mysqli_fetch_array($this->query($sql));          
        return $array;
    }
    // Closing database connection
    public function close() {
        mysql_close();
    }

}

?>

DB_Functions.php

`class DB_Functions {

private $db;

//put your code here
// constructor
function __construct() {
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {

}


/**
 * Random string which is sent by mail to reset password
 */

`

Member Avatar for diafol

IMO mysqli and PDO seem flexible and accessible enough not to require a wrapper unless you want to extend their functionality with new methods. Anyhow,
perhaps that is not for me to judge, but what exactly do you want? Do you want client code to use your wrapper class?

yes my client ask to just make a connection for him he have already made a code and asked me to modify

This was the clients code

<?php
class DB_Connect {

    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }

    // Connecting to database
    public function connect() {
        require_once 'include/config.php';
        // connecting to mysql
        $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        // selecting database
        mysql_select_db(DB_DATABASE);

        // return database handler
        return $con;
    }

    // Closing database connection
    public function close() {
        mysql_close();
    }

}

?>

I made it and the connection was made perfectly i beleive so but when i tried to call the query from database what i got

Fatal error: Using $this when not in object context in C:\wamp\www\rankdroid\index.php on line 39

this is my code

<?php
        $this->db = new DB_Connect();
        $this->db->connect();

        $connection = $this->db->connect();
        $myquery = "SELECT * FROM users";
        $query = $connection->query($myquery);        

        while($array = mysqli_fetch_array($sql)) {
            echo $array['username'] . '<br />';              
        }

        $connection->close();
    ?>

$this can only be used within an object instance, so remove it:

$db = new DB_Connect();
$db->connect();
$connection = $db->connect();
$myquery = "SELECT * FROM users";
$query = $connection->query($myquery);        
while($array = mysqli_fetch_array($sql)) {
    echo $array['username'] . '<br />';              
}
$connection->close();

IMO It's a little weird to have a database wrapper and still having to call mysqli_fetch_array.

Member Avatar for diafol

Agree with p. Not seeing any real advantage of using the wrapper. You seem to be using it to 'shorten' or 'alias' existing methods (namely 'fetch'), which is fine I suppose, but a little bit pointless for all the class actually offers. Is there anything else involved?

Well as far as I heared that OOP method is very safe as compared to core PHP so I am that is why moving forward to it. What is the suggettion you will give me.

Althou this is working now this was a mistake you solved it out for me but can you explain me what had you actually done there though ??

$db = new DB_Connect();
$db->connect();
$connection = $db->connect();
$myquery = "SELECT * FROM users";
$query = $connection->query($myquery);        
while($array = mysqli_fetch_array($sql)) {
    echo $array['username'] . '<br />';              
}
$connection->close();

And by reading your response I beleive that you are trying to say me to not use mysqli_fetch_array type of queries in OOP so instead this what should I go for little bit confused please

OOP method is very safe as compared to core PHP

OOP in itself has nothing to do with being safe. You can write very safe code with procedural PHP.

what had you actually done there though

The code you had referenced $this. It is an OOP construct, a special variable that access the current object instance. Since your code is procedural, you cannot use it. Removing it resulted in the use of regular variables.

Agreed with @pritaeas, everything is based on how you code.
Based on your Db_connect.php in the question, what you should code is:

$db = new DB_Connect();
$db->connect();
$connection = $db->connect();
$myquery = "SELECT * FROM users";
$results = $connection->fetch($myquery);        
foreach($results as $array) {
    echo $array['username'] . '<br />';              
}
$connection->close();
Member Avatar for diafol

I'm not even sure the wrapper class makes any sense. Can't see the point of storing credentials like that. Then we see procedural use of mysqli in query and fetch although an object dbc is created in the constructor. However this object is not explicitly declared. I think the class needs a re-think

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.