Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 07:03 PM   PM User | #1
Teddybear
New Coder

 
Join Date: Apr 2012
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Teddybear is an unknown quantity at this point
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..
Teddybear is offline   Reply With Quote
Old 11-07-2012, 07:24 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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);

}
?>
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 AM.


Advertisement
Log in to turn off these ads.