...

View Full Version : PHP and MySql



Teddybear
11-07-2012, 08:03 PM
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

Fou-Lu
11-07-2012, 08:24 PM
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
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
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);

}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum