PHP Code | Database I can install, but the core files doesn't have DB connectivity defined where I can put those details


#1

php-shopping-cart-with-payment-gateway-integration.zip (52.6 KB)

the above attachment has a .sql file, but I could not find the place in the php files where I can enter the details of the new database to be created.


#2

Please post the codes here. No one wants to download suspicious files on the internet.


#3

I braved it. The DB class has defined parameters. OP just needs to create a config file with the defines and include it.


#4
<?php

class DBController
{

    private $host = "HOST";

    private $user = "DB_USER";

    private $password = "DB_PASSWORD";

    private $database = "DB_NAME";

    private static $conn;

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

    public static function getConnection()
    {
        if (empty($this->conn)) {
            new Database();
        }
    }

    function getDBResult($query, $params = array())
    {
        $sql_statement = $this->conn->prepare($query);
        if (! empty($params)) {
            $this->bindParams($sql_statement, $params);
        }
        $sql_statement->execute();
        $result = $sql_statement->get_result();
        
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
        
        if (! empty($resultset)) {
            return $resultset;
        }
    }

    function insertDB($query, $params = array())
    {
        $sql_statement = $this->conn->prepare($query);
        if (! empty($params)) {
            $this->bindParams($sql_statement, $params);
        }
        $sql_statement->execute();
        
        $id = mysqli_insert_id ( $this->conn );
        return $id;
    }
    
    function updateDB($query, $params = array())
    {
        $sql_statement = $this->conn->prepare($query);
        if (! empty($params)) {
            $this->bindParams($sql_statement, $params);
        }
        $sql_statement->execute();
    }

    function bindParams($sql_statement, $params)
    {
        $param_type = "";
        foreach ($params as $query_param) {
            $param_type .= $query_param["param_type"];
        }
        
        $bind_params[] = & $param_type;
        foreach ($params as $k => $query_param) {
            $bind_params[] = & $params[$k]["param_value"];
        }
        
        call_user_func_array(array(
            $sql_statement,
            'bind_param'
        ), $bind_params);
    }
}

How? I am new to PHP.


#5

Just instantiate the class and pass in the correct arguments. You should be ok after this.


#6

Sir, Can you guide me how or perhaps give me the link on internet somewhere?


#7

Instantiating an object is one of the basics of OOP and in many cases like this quite simple:-

$db = new DBController ; // Name the object anything you like

Then you can use the object ($db) at will.


#8

Thanks. I forgot to also mention that since the OP is using properties, they have to also assign the properties as well instead of passing in arguments.

What I am trying to do is not give you the answer because you don’t learn anything from people giving you it or doing it for you. I am trying to guide you to where you can think for yourself and write the codes without people holding your hands. This is how you learn.


#9

I understand. Thanks. I will research and get back to you.


#10

That got me to start with, as the __construct has no parameters input. Then I notice that the proerty values (with the exception of $conn) are hard coded into the class, so they are already set.

On a side note, since you are going down the OOP route, why not use PDO?
Also there is quite bit of repeteition in the class methods, I’m sure you could maybe add another method that would remove much of that.


closed #12

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.