0

I've been trying to display the number of rows using this code but it keep says

1 Rows which is wrong

<?php

            $link = mysql_connect("localhost", "gamin1_forum", "password123");
            mysql_select_db("gamin1_forumdb", $link);

            $result = mysql_query("SELECT COUNT(*) FROM smf_personal_messages", $link);
            $num_rows = mysql_num_rows($result);

            echo "$num_rows Rows";

            ?>

Rows are approximately 1443 but it kept saying 1

David Dejan
  • 29
  • 1
  • 6
  • The mysql_* extension is deprecated, [don't get started with it](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – VolkerK Sep 19 '15 at 15:23

2 Answers2

0

This is true, mysql_num_rows() is telling that there was one "row" returned. You need to check the value of the returned row to get your count. Try this

$link = mysql_connect("localhost", "gamin1_forum", "password123");
$result = mysql_query("SELECT COUNT(*) as cnt FROM smf_personal_messages", $link);
$row = mysql_fetch_array($result);

echo ($row['cnt']);

On a side note, you should not use mysql_* functions for security reasons. Try PDO/mysqli_* functions

mynawaz
  • 1,599
  • 1
  • 9
  • 16
0

The Count(*) returns you one row which contains the number of rows as a value.

By using mysql_num_rows($result) you are actually counting the amount of rows of the Count(*) result which really is one.

Change it to:

$result = mysql_query("SELECT * FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);

Or just use the Count(*) value (which is probably better since it count in the DB and not retrieving the whole table for it) using mysql_fetch_array.

Tamir Vered
  • 10,187
  • 5
  • 45
  • 57