如果用MySQL声明

时间:2014-02-24 23:24:12

标签: php html mysql

尝试更改此代码,以便我可以选择哪些数字(值)显示哪些信息,这是现在的代码:

$sql_doublecheck = mysql_query("SELECT * FROM adminpage WHERE setting='main' AND close_site='1'");
$doublecheck = mysql_num_rows($sql_doublecheck);

if ($doublecheck == 0) {
    echo "<strong><font color=red>> Close site is off</font></strong>";
} else if ($doublecheck > 0) {
    echo "<strong><font color=green>> Close site is working</strong></font><br />";
}

这一切似乎都运行正常,但是在数据库&#34; close_site&#34;是一个包含4个选项的枚举&#34; 0,1,2,3和#34;我想为每个数字做一个if语句......非常简单的术语:

如果&#34; 0&#34; echo&#34;关闭网站已关闭&#34; 如果&#34; 1&#34; echo&#34;维护模式激活&#34; 如果&#34; 2&#34; echo&#34;关闭网站已开启&#34; 如果&#34; 3&#34; echo&#34;另一种选择&#34;

我知道上面的代码不是代码,但这是我希望实现的目标。我想知道它是否会像:

$sql_doublecheck = mysql_query("SELECT * FROM adminpage WHERE setting='main' AND close_site='1'");
$doublecheck = mysql_num_rows($sql_doublecheck);

if($doublecheck == 0) {
    echo "<strong><font color=red>> Close site is off</font></strong>";
} else if ($doublecheck == 1) {
    echo "<strong><font color=green>> Maintenance mode active</strong></font><br />";
} else if ($doublecheck == 2) {
    echo "<strong><font color=green>> Close site is on</strong></font><br />";
}

不太确定?感谢

1 个答案:

答案 0 :(得分:0)

将其更改为:

$query = mysql_query("SELECT * FROM adminpage WHERE setting='main'");
$result = mysql_fetch_array($query);
switch($result['close_site']) {
   case 4:
       echo 'something to show when close_site=4';
       break;
   case 3:
       echo 'something to show when close_site=3';
       break;
   case 2:
       echo 'something to show when close_site=2';
       break;
   case 1:
      echo "<strong><font color=green> Close site is working</strong></font><br />";
      break;
   case 0:
   default:
      echo 'close site is off, or none of the above';
}

在旁注中,你真的应该停止使用mysql扩展,因为它已被正式弃用,并切换到MySQLiPDO