Home » Php » sql – How to inner join on php?

sql – How to inner join on php?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to do a function in PHP, which calculates and returns the total price that a specific user needs to pay of a shopping cart.
The function returns me a value of 0, and when I checked the query array, it returns 0 records – I mean that probably the problem with this function, is that the fetch from the DB don`t work well.
When I did a regular db action (without inner join) it worked fine, but with the inner join (left join) it doesn’t work and returns 0 records.

function Total_bill($username) {
    $host = "localhost";
    $user = "root";
    $pass = "";
    $db = "ggggg";
    $query = 0;
    $total = 0;

    $database = new Database;

    $query = $database->query("
        SELECT 
            serial_number, 
            count_purchase, 
            price_item 
        FROM 
            bought_history 
            LEFT JOIN product ON bought_history.serial_number = product.serial_number 
        WHERE 
            username = username 
            and date_purchase = date_purchase");

    //echo "yout total".sizeOf($query);

    for ($i = 0; $i < count($query); $i++) {
        if ($bought_history . $serial_number->$count_purchase == 1) {
            $total = $total + $product . $serial_number->$price_item;
        } else {
            $total = $total + ($product . $serial_number->$price_item * $bought_history . $serial_number->$count_purchase);
        }
        //return $total;
    }
}
How to&Answers: