0

So I've been working on a small system which involves codes which is used for a user to redeem them for some sort of reward.

So far I have this script in PHP and HTML: http://pastebin.com/UUsEKpev

It's only showing one result which you can see here, I want it to display multiple results in a table going down showing all results.

<?php

$yn;
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("mcv") or die(mysql_error());

$result = mysql_query("SELECT * FROM Codes")
or die(mysql_error());  

$row = mysql_fetch_array( $result );
// Print out the contents of the entry 

if ($row['Used'] == 1) {
    $yn = "Yes";
}
else{
    $yn = "No";
}

?>

<html>
<head>
    <title>Minecraft Codes</title>
    <style type="text/css">

    tbody {
        display: table-row-group;
        vertical-align: middle;
        border-color: inherit;
    }

    tr {
        display: table-row;
        vertical-align: inherit;
        border-color: inherit;
    }

    th {
        border: 1px solid #C3C3C3;
        padding: 3px;
        vertical-align: top;
        width: 20%;
        background-color: #E5EECC;
    }

    table.reference {
        background-color: white;
        border: 1px solid #C3C3C3;
        border-collapse: collapse;
        width: 50%;
    }

    table.reference td {
        border: 1px solid #C3C3C3;
        padding: 3px;
        vertical-align: top;
        width: 20%;
    }

    table, th, td, input, textarea {
        font-size: 100%;
    }

    body, p, h1, h2, h3, h4, table, td, th, ul, ol, textarea, input {
        font-family: verdana,helvetica,arial,sans-serif;
    }

    </style>
</head>

<body>
    <center>
        <br />
        <h1><u>Minecraft Server VIP Codes</u></h1>
        <br />

        <table class="reference">
            <tr>
                <th>Code</th>
              <th>Used?</th>
          </tr>
          <?php echo "<tr><td>".$row['Code']."</td><td>".$yn."</td></tr>"; ?>
        </table>
    </center>
</body>
</html>
2
  • It is welcome here to add the code, and even the result in the question, to let it be complete and coherent. Commented Oct 21, 2012 at 18:54
  • mysql_fetch_array returns a single ROW of the query's results, not the whole result set. Commented Oct 21, 2012 at 19:08

3 Answers 3

1

Your problem is that you are only fetching one row:

$row = mysql_fetch_array( $result );

That line fetches the current row of the result set. You want to do that in a loop:

$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}

If you can please avoid using the mysql_ functions, they are deprecated, see the giant warning here and read this

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

1 Comment

... or display immediately without storing
1
<table class="reference">
    <tr>
        <th>Code</th>
        <th>Used?</th>
    </tr>

    <?php while ($row = mysql_fetch_array($result)): ?>
    <?php 
        if ($row['Used'] == 1) {
            $yn = "Yes";
        }
        else{
            $yn = "No";
        }
    ?>
    <tr>
        <td><?php echo $row['Code']; ?></td>
        <td><?php echo $yn; ?></td>
    </tr>
    <?php endwhile; ?>
</table>

3 Comments

Didn't know while(...): ... endwhile; was a valid syntax. I'm used to curly braces.
yes! that too exist,, i have re-edited so that others can understand with readability factor as you did. Dont worry we learn step by step and also we didnt started running when born.
Couldn't get this to work either, the results either come out wrong or the Code row doesn't display.
0

Well yes, you call mysql_fetch_row() only a single time, thus you only retrieve a single row. You have to wrap that into a loop that is executed as many times as there are rows.

There are millions of examples on the internet that you can learn from...

Comments

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.