Jump to content

I have a question


Renlok

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.
Link to comment
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]
Link to comment
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
Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.