0

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

Hour Grim
  • 45
  • 7
  • 1
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jan 23 '18 at 00:06
  • Don't use quotes around column or table names. use backticks – juergen d Jan 23 '18 at 00:06
  • That query also won't work because it is not valid syntax – John Conde Jan 23 '18 at 00:07
  • I don't think I am using new codes? To be honest I am a terrible learner when it comes to new stuff so I never switched / moved on. – Hour Grim Jan 23 '18 at 00:07
  • 1
    You never fetch your results – John Conde Jan 23 '18 at 00:08
  • @JohnConde What do you mean it's not a valid syntax? I guess I don't follow. – Hour Grim Jan 23 '18 at 00:08
  • @HourGrim He means [this](https://stackoverflow.com/questions/9931684/fetch-all-rows-based-on-the-query-into-an-array) – hungrykoala Jan 23 '18 at 00:16

1 Answers1

3

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);
}
?> 
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thanks for your help John, I did it a different way and got the same result thanks to you and @juregen d – Hour Grim Jan 23 '18 at 00:22