-1

I'm faced with a little problem, I want to count the total number of rows from a db table. I try this queries, but with no success:

$nr = mysql_query("select [TotalCount]=rows from $tablename where ID=object_ID('gyuruszam')");

$nr = mysql_query("SELECT COUNT(gyuruszam) FROM $tablename");

$nr = mysql_query("SELECT gyuruszam FROM $tablename, st.row_count");

Please help me out on this, thank you very much in advance!

styven
  • 51
  • 1
  • 9
  • `mysql_query` returns a handle to the result, not the result itself. See PHP docs. – bidifx Jul 02 '14 at 10:55
  • 2
    don't use mysql_* functions they are deprecated use PDO or mysqli – deW1 Jul 02 '14 at 10:55
  • Also, grab phpMyAdmin, or just http://SQLFiddle.com, and check your SQL queries before trying to integrate them in PHP if you're not familiar with how they work (no idea what that first query is supposed to be...) – IMSoP Jul 02 '14 at 11:00

6 Answers6

2

You need to give the result of the COUNT a name using AS, e.g., numrows:

$result = mysql_query("SELECT count(gyuruszam) as numrows from $tablename");
$data = mysql_fetch_assoc($result);
echo $data['numrows'];
middus
  • 9,103
  • 1
  • 31
  • 33
2
query("SELECT COUNT(*) FROM " . $tablename)

When using *, it includes possible null values. When using COUNT(gyuruszam) and gyuruszam contains null, those rows won't get counted.

mysql_* functions are deprecated, use PDO or at least mysqli instead.

Personally I suggest not to put a variable straight into a quoted string as it may lead to unexpected behaviour in some situations.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • I'm not sure what you mean by your last sentence; in your example, you use `"some stuff " . $variable`; since you're using double quotes anyway, there's absolutely no difference between that and `"some stuff $variable"`. – IMSoP Jul 02 '14 at 11:05
  • @IMSoP It's way cleaner coding style not to put variables into quoted strings. What do you expect here: `$a = 'a'; $aa = 'b'; echo "$aaa$a$aa";` its dirty code style. The stackoverflow code highlighting also shows that. – Daniel W. Jul 02 '14 at 11:08
  • I expect it to look up a variable called $aaa, because variables aren't limited to one letter in PHP. I grant that it *can* be ambiguous, and respect that you consider it bad style, but I don't think a blanket ban on using it, with no explanation, is particularly relevant or useful in this answer. – IMSoP Jul 02 '14 at 11:13
  • @IMSoP I can agree with your last comment and modified my answer to make it more clear it's not something strict. – Daniel W. Jul 02 '14 at 11:17
1

Simply Try This:

$count = mysql_num_rows(mysql_query("SELECT * FROM $tablename"));
Dinesh G
  • 244
  • 1
  • 13
  • 2
    This will work, but be incredibly inefficient, since the entire content of the table must be passed from MySQL to PHP. Using COUNT(*) on the DB lets MySQL tell you the answer without even looking at what the table contains. – IMSoP Jul 02 '14 at 11:09
  • yes, it's working, sorry for missunderstanding the code.. thank you very much! I supplemented a little the query, like this: `$rowcount = mysql_num_rows(mysql_query("SELECT gyuruszam FROM $tablename"));` – styven Jul 02 '14 at 11:16
0

For getting the number of rows in a table use mysql_num_rows()

Also mysql_* are deprecated from MySQL 5.5

Take few minutes to read this post

Try this,

$result = mysql_query("SELECT * FROM $tablename");
echo mysql_num_rows($result);
Community
  • 1
  • 1
Ranjith
  • 2,779
  • 3
  • 22
  • 41
  • That will echo 1: the result of the query is one row containing a number. – IMSoP Jul 02 '14 at 10:57
  • Thanks for pointout my mistake. I just copied and paste it directly. I edited now – Ranjith Jul 02 '14 at 11:00
  • 2
    The count(*) version will be much more efficient than fetching all the rows into PHP, you just needed to output the result, not the size of the result. – IMSoP Jul 02 '14 at 11:02
0

mysql_query("SELECT COUNT(*) as rowCount FROM $tablename") this should return an integer with number of records in $tablename

Caweren
  • 236
  • 1
  • 9
0
$result = mysql_query("SELECT COUNT(*) as totalCount FROM $tablename");
$row = mysql_fetch_array($result);
echo $row['totalCount'];
Vamsi Krishna
  • 161
  • 10