Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: PHP and MySql

  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP and MySql

    I'm not sure if I should be in the PHP forum or the MySql forum. I am trying to display a table from a MySql database using PHP, there should be 5 items, but only 4 seem to be displaying, what am I doing wrong?

    <start code>
    <?php
    require_once 'connectdb.php';

    // Retrieve the list of pans from the database
    $query = "SELECT * FROM pans";


    //Submit the query to the database.

    $results = @mysql_query($query, $connection);
    if (!results) {
    die(mysql_error());
    }

    // How many items do we have? Store this for future reference
    $num_pans = mysql_num_rows($results);

    ?>
    <end code>

    Some general HTML, then:

    <start code>
    <?php
    if ($num_pans > 0) {
    $pans = mysql_fetch_assoc($results);

    $panname = ($row['panname']);
    $stock = ($row['stock']);
    $price = ($row['price']);

    ?>

    <!-- Create Stock table structure -->
    <table border=1, cellpadding=3>

    <!-- Loop through the items in the pans table -->
    <?php for ($i=0; $i<$row_num; $i++)
    {
    $row = mysql_fetch_assoc($results);
    }; ?>


    <tr><th>Pan Name</th><th>In Stock</th><th>Price (pence)</th></tr>
    <tr><td><?php echo $row['panname']; ?></td>
    <td> <?php echo $row['stock']; ?></td>
    <td> <?php echo $row['price']; ?></td></tr>

    <?php

    //Print out the items in the table loop

    while ($row = mysql_fetch_assoc($results)) {
    echo "<tr>";

    foreach($row as $cell)
    echo "<td>$cell</td>";
    echo "</tr>";

    }

    // close the connection
    mysql_close($connection);

    }
    ?>
    <end code>

    What I am getting is

    Pan Name | In Stock | Price (pence)

    coppersaucepan | 10 | 1988
    coppersaute | 20 | 1299
    steelcasserole | 8 | 2099
    steelfrying | 22 | 899

    It appears to be missing off the first item, eg:
    coppercasserole | 20 | 2199

    Please help. Thanks
    Last edited by Teddybear; 11-07-2012 at 07:18 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You keep fetching where you don't need to. You have three fetches in total, the one in the loop is the useful one, the one prior is manually printed, and the one for $pans is completely abandoned.

    Replace this:
    PHP Code:
    <?php
    if ($num_pans 0) {
    $pans mysql_fetch_assoc($results);

    $panname = ($row['panname']);
    $stock = ($row['stock']);
    $price = ($row['price']);

    ?>

    <!-- Create Stock table structure -->
    <table border=1, cellpadding=3>

    <!-- Loop through the items in the pans table -->
    <?php for ($i=0$i<$row_num$i++)
    {
    $row mysql_fetch_assoc($results);
    }; 
    ?>


    <tr><th>Pan Name</th><th>In Stock</th><th>Price (pence)</th></tr>
    <tr><td><?php echo $row['panname']; ?></td>
    <td> <?php echo $row['stock']; ?></td>
    <td> <?php echo $row['price']; ?></td></tr>

    <?php

    //Print out the items in the table loop

    while ($row mysql_fetch_assoc($results)) {
    echo 
    "<tr>";

    foreach(
    $row as $cell)
    echo 
    "<td>$cell</td>";
    echo 
    "</tr>";

    }

    // close the connection
    mysql_close($connection);

    }
    ?>
    With this:
    PHP Code:
    <?php
    if ($num_pans 0) {
    ?>
    <!-- Create Stock table structure -->
    <table border="1" cellpadding="3">
    <tr><th>Pan Name</th><th>In Stock</th><th>Price (pence)</th></tr>

    <!-- Loop through the items in the pans table -->
    <?php
    while ($row mysql_fetch_assoc($results))
    {
        print (
    '<tr>');
        
    printf('<td>%s</td>'implode('</td><td>'$row));
        print (
    '</tr>');
    }

    // close the connection
    mysql_close($connection);

    }
    ?>


  •  

    Posting Permissions

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