dongya8378 2018-01-23 00:06
浏览 55
已采纳

MySQL和PHP如果/不显示输出/回显[重复]

This question already has an answer here:

Alright so I am trying to make a page in my Admin control panel where you can view if the site is under some sort of maintenance. I am having trouble, not sure if it's from MySQL end, human error, or PHP.

Here is my code

<?php
$getMaint = mysql_query("SELECT * FROM 'system'");

      if($getMaint['maintenance'] == 1){ echo "<b align='center'>Maintenance is turned <j style='color:#00A808'>On</j></b>";
      }  else { echo "<b align='center'>Maintenance is turned <j align='center' style='color:#CC000A'>Off</j></b><br /><br />   "; }



    ?> 

What I am getting on my end from it is either when having maintenance either 1 or 0 it shows Off no matter if in my database I change it from 0 or 1?

I am not sure if it's because the structure of maintenance number system is setup as a enum('0', '1')

What I want it to do is when maintenance is marked as 1 in database(MySQL) I want it to say On for maintenance or if 0 it will say Off.

---------------------------------Fixed---------------------------------------

My solution:

<?php
$getSystem = mysql_query("SELECT * FROM `system`");
while($Maint = mysql_fetch_array($getSystem))
{
      if($Maint['maintenance'] == 1){ echo "<b align='center'>Maintenance is turned <j style='color:#00A808'>On</j></b>";
      }  else { echo "<b align='center'>Maintenance is turned <j align='center' style='color:#CC000A'>Off</j></b><br /><br />   "; }
    }


    ?>

Thank you John Cone and juergen d

</div>
  • 写回答

1条回答 默认 最新

  • doutangguan2460 2018-01-23 00:16
    关注

    There are many issues with this code:

    1. You use the deprecated mysql API which was removed in PHP 7. It should never be used anymore. You should be using mysqli or PDO. I'll use mysqli here since it's easier to continue to use your code as an example.
    2. You use single quotes around your table name which is incorrect. You can use ticks or nothing.
    3. You never fetch the results of your query
    4. You don't check for errors. How do you know if anything went wrong, or what went wrong, if you don't bother to check?

    .

    <?php
    $conn = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
    $result = mysqli_query($conn, "SELECT * FROM `system`");
    if ($result) {
        $getMaint = mysqli_fetch_array($result);
        if($getMaint['maintenance'] == 1){ 
           echo "<b align='center'>Maintenance is turned <j style='color:#00A808'>On</j></b>";
        }  
        else { 
            echo "<b align='center'>Maintenance is turned <j align='center' style='color:#CC000A'>Off</j></b><br /><br />   "; 
        }
    }
    else {
        echo mysqli_ewrror($result);
    }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?