SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple mysql_fetch_array Conditions?

    Hi guys, is it possible to have two conditions for a while loop? Please see my code below.

    PHP Code:
    <html>
        <head>
            <title>Print Database</title>
    <style type="text/css">
    body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    </style>
        </head>
        <body>
        <?php
    include_once('connection.php');
    $query "SELECT * FROM products_description ORDER BY products_name";
    $result mysql_query($query) or die (mysql_error());

    $query1 "SELECT products_price FROM products";
    $result1 mysql_query($query1) or die (mysql_error());

    //Print out Results
    while($row mysql_fetch_array($result),$row1 mysql_fetch_array($result1)){
        echo 
    "<div style='width: 600px; background-color: #CCCCCC; margin-bottom: 10px; padding: 4px 4px 4px 4px;'>";
        echo 
    "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; width: 40px; float: left;'>".$row['products_id']."</div>";
        echo 
    "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; width: 40px; float: left;'>".$row1['products_price']."</div>";
        echo 
    "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; width: 370px;'>".$row['products_name']."</div>";

        echo 
    "</div>";
    }

    ?>
        </body>
    </html>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    while($row = mysql_fetch_array($result) && $row1 = mysql_fetch_array($result1)){

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code:
    while($row = mysql_fetch_array($result) && $row1 = mysql_fetch_array($result1)){
    Hi, I can see this should work but nothing seems to be displaying now from $row?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Both conditions have to be true. If one of the two fetches doesn't return a row, the loop isn't executed.

    I think your problem is the two separate queries. How can you be sure the two fetches get rows that are related to eachother? Something tells me you should use one query, joining the two tables.

    What is the relation between the products table and the products_description table?

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, ok I think I've worked out how to join the tables together. Everything is now displaying but I'm getting around 100 of each product instead of just the one in the database. I'm working on a work website, recently taken it over from the previous guy. My code now looks like:

    <html>
    <head>
    <title>Print Database</title>
    <style type="text/css">
    body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    </style>
    </head>
    <body>
    <?php
    include_once('connection.php');
    $query = "SELECT products.products_id, products_description.products_name, products.products_price FROM products, products_description";
    $result = mysql_query($query) or die (mysql_error());

    //Print out Results
    while($row = mysql_fetch_array($result)){
    echo "<div style='width: 600px; background-color: #CCCCCC; margin-bottom: 10px; padding: 4px 4px 4px 4px;'>";
    echo "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; width: 40px; float: left;'>".$row['products_id']."</div>";
    echo "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; width: 370px;'>".$row['products_name']."</div>";
    echo "<div style='font-size: 12px; font-family: Arial, Helvetica, sans-serif; width: 40px; float: right;'>".$row['products_price']."</div>";
    echo "</div>";
    }

    ?>
    </body>
    </html>

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    SELECT
        products.products_id
      , products_description.products_name
      , products.products_price
    FROM products
    INNER JOIN products_description
    ON products.products_id = products_description.products_id
    You forgot the join condition. If you don't specify on what fields the join has to be done, the result will be a cross join: each row from table 1 will be joined to each row from table 2. So if table 1 has 10 rows, and table 2 has 5 rows, the query result will have 50 rows (10 x 5).

    Above I assumed the two tables are to be joined on the products_id column. But I might be wrong. You didn't post the table definitions, and you didn't tell me what the relation between the two tables is.

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code:
    SELECT
        products.products_id
      , products_description.products_name
      , products.products_price
    FROM products
    INNER JOIN products_description
    ON products.products_id = products_description.products_id
    You forgot the join condition. If you don't specify on what fields the join has to be done, the result will be a cross join: each row from table 1 will be joined to each row from table 2. So if table 1 has 10 rows, and table 2 has 5 rows, the query result will have 50 rows (10 x 5).

    Above I assumed the two tables are to be joined on the products_id column. But I might be wrong. You didn't post the table definitions, and you didn't tell me what the relation between the two tables is.
    That's DONE IT! Perfect, thank you for your help. People are so quick and accurate in reply on this forum, I really do value it. Thanks again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •