1

I have a query like the following:

mssql_query("SELECT COUNT(*) FROM PRTL_PasswordSecurityQuestions where isPublished = 1 ")

I need to display some content when count is greater than 3. For that I have put the statement like this.

if(mssql_query("SELECT COUNT(*) FROM PRTL_PasswordSecurityQuestions where isPublished = 1 ")>=3)
{......}

Is it correct way to check.Anyway its not working for me.

Techy
  • 2,626
  • 7
  • 41
  • 88
  • fetch it first. then make your comparison – Kevin Apr 16 '15 at 13:24
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 16 '15 at 13:24
  • So you mean this comparison is wrong ..right @Ghost – Techy Apr 16 '15 at 13:24
  • first assign query result into variable, and then get count of rows from variable. I dont know what `mssql_query` returns in php – Jemshit Apr 16 '15 at 13:25
  • @Techy yes, your basically, comparing zero to a resource – Kevin Apr 16 '15 at 13:25
  • @JayBlanchard actually, its the `mssql` API, but i agree, i'd take PDO any day – Kevin Apr 16 '15 at 13:26
  • Oops @Ghost ¯\\_(ツ)_/¯ – Jay Blanchard Apr 16 '15 at 13:27
  • mssql is like mysql,but my database is sql server – Techy Apr 16 '15 at 13:27

1 Answers1

4

You can't directly compare it inside your if condition:

if(mssql_query("SELECT COUNT(*) FROM PRTL_PasswordSecurityQuestions where isPublished = 1 ")>=3)
{......}

Since it returns a result set, you need to fetch it first, after getting the count and stored, then you make your comparison:

$query = mssql_query("SELECT COUNT(*) AS total FROM PRTL_PasswordSecurityQuestions WHERE isPublished = 1";
$row = mssql_fetch_assoc($query); // fetch it first

if($row['total'] > 3) {
    // do what you have to do
}
Kevin
  • 41,694
  • 12
  • 53
  • 70