-1

This code is supposed to show the number of rows in the column "id". Why isn’t is working, when I go to the page it shows nothing except my HTML stuff?

<?php
$con = mysql_connect("quollcraft.net", "quollcr1_forum", "password");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("quollcr1_hub", $con);

$result = mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 1 ");
echo "<table>";
while ($row = mysql_fetch_array($result)) {
    echo "<td>";
    echo "<center>";
    echo '<p>' . $row ['id'] . ' total users<p>';

    echo "</td>";

    }
echo "</table>";    

mysql_close($con);
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    What, exactly, does it produce? – Hamish May 04 '14 at 06:55
  • 1
    Use mysql_error() to check if query is correct. – Noman Ghani May 04 '14 at 06:57
  • mysql is deprecated use other extension like mysqli or pdo – Manwal May 04 '14 at 06:59
  • You're putting a lot of trust in your browser too. Add at least one table row in your html table, close the center and p tags. Also if you want the number of rows in the column ID, you may want to use COUNT(id) in your query. That will produce a single row, not all the rows. – pah May 04 '14 at 07:02
  • @rid The credentials don’t work. But if the `password` was truly `password` there’s not much anyone can do to save that. – Giacomo1968 May 04 '14 at 07:04

4 Answers4

2

Your table name you are using are just table. I guess it should be changed to the real table name from which you want your data

Cleric
  • 3,167
  • 3
  • 23
  • 24
2

table is one of the reserved word(s) in MySQL. You need to wrap them using backticks.

Like this..

$result = mysql_query("SELECT id FROM `table` ORDER BY id DESC LIMIT 1 ");
                                      ^     ^  //<---- Like that

This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, Prepared Statements of MySQLi or PDO_MySQL extension should be used to ward off SQL Injection attacks !

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 2
    There is also a good chance `table` in this original poster’s question is simply a boiler plate query they found somewhere so `table` should be something else entirely. – Giacomo1968 May 04 '14 at 07:00
1

Others have decent MySQL tips, but what happens when you view the source code? Because the HTML seems to be broken at best. This is the cleaned up version based on what I am seeing.:

echo "<table>";
echo "<tr>";
while ($row = mysql_fetch_array($result)) {
    echo "<td>";
    echo "<center>";
    echo '<p>' . $row ['id'] . ' total users<p>';
    echo "</center>";
    echo "</td>";
}
echo "</tr>";
echo "</table>";

You were missing the table row tags <tr> & </tr> as well as the closing </center> tag.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

Looks like you are trying to retrieve the total count of ID(users) but you missed out the keyword count. You can modify either the query or you can get the count of $result. Hope this helps.