Skip to content
Advertisement

PHP select query print out numerous items from database [closed]

My Table

So I want it to print off like

Coffee Name:
ColumbianPrice: 7.99
Total Sold: 3
Total earnings: 23.9

And then to repeat for each type of coffee

Is there any web pages that allow me to do this

$a_result = mysqli_query($connection_var, "SELECT COF_NAME, PRICE FROM coffees");
while ($rows = mysqli_fetch_array($a_result)){
    print("<p>");
    print($rows[0]);
        print("<p>");

    print($rows[1]);
        print("<p>");

    print($rows[2]);
        print("<p>");

    print($rows[3]);
        print("<p>");

    print($rows[4]);
    print("</p>");
}

Currently, this is what I have but know it is not correct. Any help much appreciated?

Advertisement

Answer

You might need to update the SQL query first, in order to retrieve data in the manner you want.

so let’s say if your database table looks like below

Table: coffees

id    coffee_name    columbia_price   total_sold    total_earnings
-------------------------------------------------------------------
1     Raw Coffee     7.99             3             23.9

Your SQL query should be:

SELECT
`id` as `id`
`coffee_name` as 'Coffee Name:',
`coffee_price` as 'Columbia Price:',
`total_sold` as 'Total Sold:',
`total_earnings` as 'Total Earnings:',
from `coffees`;

And the expected output of this query should be

[
    [
        "id" => 1,
        "Coffee Name:" => 'Raw Coffee',
        "Columbia Price:" => '7.99',
        "Total Sold:" => '3',
        "Total Earnings:" => '23.9'
    ]
]

If you get the expected result, then PHP implement should be:

while($row = mysqli_fetch_assoc($result)) {
    foreach ($row as $key => $value) {
        echo "<p>". $key ." ". $value ."</p>";
    }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement