简体   繁体   中英

Is it possible to specify the sql select query inside if condition

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM