0

Will someone PLEASE show me what I'm doing wrong? I'm new to using phpMyAdmin and db.

I have a db table with 4 items. Looks fine on a plain php page - I get all four items in correct order.

But if I use coding <ul class="column"><li><div class="imgblock"> like shown below, I get 4 duplicates of my last entry in the db table. I think I have to rearrange how I use ", ', and ), etc.? Not sure how though...

<?php
// Make a MySQL Connection
mysql_connect("localhost", "....", "....") or die(mysql_error());
mysql_select_db("....") or die(mysql_error());

$show = "SELECT pn, pgname, img, name, price FROM prodshort";
$result = mysql_query ($show);
while ($show = mysql_fetch_array ($result))
{
$field2= $show['pn'];
$field3= $show['pgname'];
$field4= $show['img'];
$field5= $show['name'];
$field6= $show['price'];
$field7= $show['specs'];
}
?>

<!-- start -->
<ul class="column"><li><div class="imgblock">
<a href="<?echo "$field3";?>">
<img src="<?echo "$field4";?>" width="320" height="240" alt="<?echo "$field5";?>" /></a></div><br />
<a href="<?echo "$field3";?>"><?echo "$field5";?></a>
<ul class="specs">
<?echo "$field7";?></ul>
<div class="price">
#<?echo "$field2";?>&nbsp;&nbsp;&nbsp;$<?echo "$field6";?>
</div></li></ul>
<!-- end -->
2
  • You should display items inside while loop. You sets some values to variables inside WHILE loop and overwrites existing ones. It means at the end of the loop you have variables with data fetched from the last DB record. Or I'm wrong because you didn't post full code. Commented Feb 2, 2012 at 20:59
  • What does this have to do with phpMyAdmin? Commented Feb 2, 2012 at 21:01

1 Answer 1

2

The above is probably not the code from the example, as the while loop is outside your items.

It should be more like this:

while ($show = mysql_fetch_array ($result)) {
    $field2= $show['pn'];
    $field3= $show['pgname'];
    $field4= $show['img'];
    $field5= $show['name'];
    $field6= $show['price'];
    $field7= $show['specs'];
?>
<!-- start -->
<ul class="column">
    <li>
        <div class="imgblock">
            <a href="<?echo "$field3";?>">
                <img src="<?echo "$field4";?>" width="320" height="240" alt="<?echo "$field5";?>" />
            </a>
        </div>
        <br />
        <a href="<?echo "$field3";?>"><?echo "$field5";?></a>
        <ul class="specs">
            <?echo "$field7";?>
        </ul>
        <div class="price">
        #<?echo "$field2";?>&nbsp;&nbsp;&nbsp;$<?echo "$field6";?>
        </div>
    </li>
</ul>
<!-- end -->
<?
// end of while loop
}
?>

(You might find it easier to debug if you indent your code correctly, most editors do that for free ;-)

Sign up to request clarification or add additional context in comments.

3 Comments

I will try what konsolenfreddy has suggested.
Thx, konsolenfreddy. That worked perfectly. I have another question - if I want to insert data into my db table but don't want it to show up in my php page above yet, should I use another table for that? This would be inventory that I'm not ready to list yet.
I personally would put it in the same table, with a field like hidden being set. Query for all entries which have hidden < 1. Please also see the FAQ regarding accepting answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.