1

I used a PHP code to show results of this query:

SELECT COUNT(*) FROM mytable

I want to get the number of rows in a specific table. This query works fine in phpMyAdmin, but it doesn't work when I place it in a template file.

I mean it doesn't return the number of rows.

Do you have any idea?

  • 1
    Could you post the code how you tried to retrieve the result? – Dharmender Jul 24 '12 at 5:50
  • Please post the complete code not only the SQL. and are you using db_query() or mysql_query ? – GoodSp33d Jul 24 '12 at 6:26
  • Realyl Thanks, specially kantu. I used db_query and it fixed my problem. – Zim3r Jul 24 '12 at 6:57
2

A template file should never contain code that executes a query; that should be done in a preprocess function, eventually.

The code to get the number of rows in a table is similar to the following one.

$count = db_result(db_query("SELECT COUNT(*) FROM {mytable}"));

The table name should always be put between curly brackets, which will be replaced with the prefix currently set for database tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.