Jump to content

Archived

This topic is now archived and is closed to further replies.

Renlok

I have a question

Recommended Posts

If you have a database with a number of values in it and then if you take them from the database with php how would you have it so you have a table showing each value in the database, showing number that each particular value has been entered.

Thanks for any help.

Share this post


Link to post
Share on other sites
you lost me with the part about "showing number that each particular value has been entered", but i'm guessing you simply mean echoing them in chronological order. first off, you need to have an id column that is auto_incremented that you can then reference to see what order they are in. once that is in place, just do the following:
[code]
<?php
$sql = mysql_query("SELECT * FROM myTableName ORDER BY id");
echo "<table>\n";
if (mysql_num_rows($sql) > 0) {
  while ($x = mysql_fetch_array($sql)) {
    echo "<tr>\n";
    echo "<td>$x[id]</td>\n";
    echo "<td>$x[value]</td>\n";
    echo "</tr>\n";
  }
} else {
  echo "<tr><td>No records returned</td></tr>\n";
}
echo "</table>\n";
?>
[/code]

Share this post


Link to post
Share on other sites
oh no i now how to do that, sorry for not making it clear with "showing number that each particular value has been entered"
Ok urm heres an example of what i what it to do:
The Database table:

ID          ¦        Name
1          ¦          John
2          ¦          Mike
3          ¦          Joe
4          ¦          Mike
5          ¦          John
6          ¦          John

That should then show:

Rank  ¦  Name  ¦  Enties
--------------------------
1        ¦  John    ¦    3
2        ¦  Mike    ¦    2
3        ¦  Joe      ¦    1

Hope thats clearer

Share this post


Link to post
Share on other sites
[code]<?php
<?php
$sql = mysql_query("SELECT COUNT('ID') as Number, Name
    FROM myTableName
    GROUP BY Name
    ORDER BY number DESC");
echo "<table>\n";
if (mysql_num_rows($sql) > 0) {
    echo "<tr>\n";
    echo "<td>Rank</td>\n";
    echo "<td>Name</td>\n";
    echo "<td>Entries</td>\n";
    echo "</tr>\n";
  $count = 0;
  while ($x = mysql_fetch_array($sql)) {
    $count++;
    echo "<tr>\n";
    echo "<td>$count</td>\n";
    echo "<td>$x[Name]</td>\n";
    echo "<td>$x[Number]</td>\n";
    echo "</tr>\n";
  }
} else {
  echo "<tr><td>No records returned</td></tr>\n";
}
echo "</table>\n";
?>
?>[/code]

Share this post


Link to post
Share on other sites
ok i changed the code to work with my page
[code]<?php                                                                                          # line 115
                $sql = mysqli_query("SELECT COUNT('ID') as Number, searchterm 
                    FROM search
                        GROUP BY searchterm
                        ORDER BY number");
                    echo "<table>\n";                                                          # line 120
                if (mysqli_num_rows($sql) > 0) {
                    echo "<tr>\n";
                    echo "<td>Rank</td>\n";
                    echo "<td>Search Term</td>\n";
                    echo "<td>Entries</td>\n";                                                  # line 125
                    echo "</tr>\n";
                $count = 0;
                while ($x = mysqli_fetch_array($sql)) {
                    $count++;
                    echo "<tr>\n";
                    echo "<td>$count</td>\n";
                    echo "<td>$x[Searchterm]</td>\n";
                    echo "<td>$x[Number]</td>\n";
                    echo "</tr>\n";
                }
            } else {
                echo "<tr><td>No records returned</td></tr>\n";
            }
            echo "</table>\n";
            ?>                                                                                  # line 140
</center>
    <?php
    mysqli_free_result($result);
    $db->close();
    ?>                                                                                          # line 145[/code]

comes out with the errors

[quote]Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/renlok/public_html/topsearch.php on line 119

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/renlok/public_html/topsearch.php on line 121
No records returned

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /home/renlok/public_html/topsearch.php on line 143[/quote]

Share this post


Link to post
Share on other sites
obviously your missing a parameter in your query.
when using mysqli your first parameter must be your mysqli connection.
example:

[code=php:0]
$link = mysqli_connect("localhost", "user", "password", "db");
$result = mysqli_query($link, "SELECT blah FROM table");
[/code]

hope this helps.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.