如何在PHP函数中从MySQL返回TRUE或FALSE?

时间:2012-05-15 12:31:06

标签: php function if-statement

我正在尝试使用PHP。这是我的func.php

function returnBlankStylist(){

$sql=mysql_query("SELECT * FROM `".$dbn."`.`stylist` WHERE `stylist`.`customer_id`='1'");
if (mysql_affected_rows()==0) {
 return false;
} else {
 return true;
}
}

这是我的page.tpl

<?php if (returnBlankStylist==false){?>
<div class="center graybox"> Please enjoy this discount since this you have never filled out a query before</div>
<? }?>

如果客户Id 1存在,则显示该消息,如果客户Id 1不存在则显示消息?

3 个答案:

答案 0 :(得分:4)

Page.tpl

<?php if (returnBlankStylist() == false) : ?>
    <div class="center graybox">
        Please enjoy this discount since this you have never filled out a query before
    </div>
<? endif; ?>

<?php if (!returnBlankStylist()) : ?>
    <div class="center graybox">
        Please enjoy this discount since this you have never filled out a query before
    </div>
<? endif; ?>

<?php if (returnBlankStylist() == false) { ?>
    <div class="center graybox">
        Please enjoy this discount since this you have never filled out a query before
    </div>
<? } ?>

答案 1 :(得分:1)

你错过了父母身份..

if(returnBlankStylist()==false){
    //your code...
}

希望它适合你

答案 2 :(得分:1)

请将您的代码if (mysql_affected_rows()==0)替换为if (mysql_num_rows()==0);

试试这个:

<?php if (!returnBlankStylist()){?>
<div class="center graybox"> Please enjoy this discount since this you have never filled out a query before</div>
<? }?>