Pregunta

This one is a tough one to crack for me personally

My dilemma is:

I have 3 InnoDB table in my database called "order_detail", "orders", "billing" with the following structure:

The table "order_detail" looks like this:
•orderid (foreign key pointing to a serial in a table called "orders")
•productid (foreign key pointing to the serial in a table called "products")
•quatity (type INT)
•price (type FLOAT)

The table "orders" looks like this:
•serial (primary key, auto_increment)
•date (type DATE)
•customerid (type INT(11)) (foreign key pointing to the serial in a table called "billing")
•total (type FLOAT)

The table "billing" looks like this:
•serial (primary key, auto_increment)
•name (type VARCHAR(25))
•userid (type VARCHAR(25))

I have a PHP file with the aim to simply print out a description of the orders placed, e.g.:

Order ID | Product ID | Amount | Quantity | Total | Date Ordered

Now, I simply want to use the userid, call my database (only the "billings" table) and be able to get the serial, date and total from the "orders" and from their onwards to get the productid, quantity and price from the "order_detail" table.

Right now, my non-working code looks like this:

<?php 
        require("includes/config.php");
        $users_query=mysql_query("SELECT * FROM billing WHERE userid='$username'");
        $row=mysql_fetch_array($users_query);
        ?>

        <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
        <?php {
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Order ID</td><td>Product ID</td><td>Amount</td><td>Quantity</td><td>Total</td><td>Date Ordered</td></tr>';
           ?>
        <tr bgcolor="#FFFFFF">
            <td><?php echo $row['serial']; ?></td>
            <td><?php echo $row['productid']; ?></td>
            <td><?php echo $row['price']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['total']; ?></td>
            <td><?php echo $row['date']; ?></td>
        </tr>
        <?php } ?>
        </table>

(How) can I retrieve the columns mentioned without making separate calls to the "order_details" and "order" tables? When I check phpMyAdmin, the InnoDB foreign key links seem to work fine. Many thanks in advance!

¿Fue útil?

Solución

Accessing foreign data can only be done using ORMs. They can inspect the DB structure and mine the data you need on the fly. In this case, they will handle the queries for you.

But i imagine you want / need to handle your data by hand the old way.

You can achieve this by making a joint query, like for example :

SELECT o.serial AS serial, d.productid AS productid, d.price AS price, d.quantity AS quantity, o.total AS total, o.date AS date
FROM order_detail d LEFT JOIN orders o ON o.serial = d.orderid LEFT JOIN billing b ON b.serial = o.customerid
WHERE b.userid = '$username';

By the way, one shouldn't store in a DB a value that can be calculated on the fly (example: the order.total column that is a SUM() of related prices and quantities).

In my own opinion, you shouldn't use hand made queries like this. It exposes you to SQL injection, data formatting / typing issues and such mess.

Otros consejos

order_details(table)

  • order_detaild_id(primary key)
  • order_id(fkey from orders table)
  • quantity price

orders table

  • order_id
  • prod_id(fkey from products)
  • date
  • userid(fkey from billing)
  • total

billing table

  • billing_id(prmiary key)
  • serial
  • name
  • userid
  • orderid

here are some changes that need to be done to your database table schema

Query should look like this:

select 
    b.serials as serial, b.user_id as user_id, b.serial as serial_id, o.product_id as     product_id, 
    od.price as price, od.quantity as quantity, o.total as total
from billings b
left join orders o 
    on o.order_id = b.order_id
left join order_details od 
    on od.order_id = o.order_id  
where b.user_id = '.$userid.';`
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top