/    Sign up×
Community /Pin to ProfileBookmark

MYSQL/PHP question

Actually this is kinda related to MYSQL but I always need it in PHP!
For example I have 3 tables:

  • Product (ProductID, name)

  • Package (PackageID, name)

  • Price (PriceID, ProductID, PackageID, price)
  • Now in my layout I want it to show the name of the package, the name of the product and then the price!

    What I do and I am sure it is completely incorrect is I have to write 3 queries!

    SELECT * FROM PRICE …
    then once I get the packageID and the productID

    again I write
    SELECT * FROM product ..
    SELECT *
    FROM package ..

    is there anyways to make a view or Stored Procedure that can do all of this in one query?! for example make a view with the following

    product name, package name, price and perhaps priceID …

    ? thanks alot!

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogMay 07.2007 — You should be able to do it in one query, something like:
    [code=php]
    <?php
    $sql = "
    SELECT Package.name AS package_name,
    Product.name AS product_name,
    Price.price AS price
    FROM Price, Package, Product
    WHERE Price.PackageID = Package.PackageID AND
    Price.ProductID = Product.ProductID
    ORDER BY Package.name, Product.name
    ";
    $result = mysql_query($sql) or die("Query failed ($sql): " . mysql_error();
    echo "<table>n";
    while($row = mysql_fetch_assoc($result))
    {
    printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>n",
    $row['package_name'],
    $row['product_name'],
    $row['price']);
    }
    echo "</table>n";
    [/code]
    Copy linkTweet thisAlerts:
    @XeroSiSauthorMay 07.2007 — thanks alot!
    ×

    Success!

    Help @XeroSiS spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...