Change page based on MySQL result

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello all,

Is there a way to get a page to change based on a MySQL query?

For instance...
If an entry for "is_maintenance" is set to 1 to display a maintenance page

While if it's set to 0 to display a separate page, the home page.

Thanks.
 

LeCeejay

Member
Dec 25, 2010
122
17
From what I can guess is it'd be something like this;
*Connect to you Database*
$maintenance = *Run a query to select the collum which is "is_maintenance"*

if $maintenance == 1
{
redirect thing or w.e
}
else
{
your page
}

Or something, idk
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("joshwelford", $con);
 
$result = mysql_query("SELECT `value` FROM `cms_settings` WHERE `id` = 'site_closed'");
$row = mysql_fetch_row($result);
$read = $row;
if($read = "eefef"){
    echo "YES";
}
while($row = mysql_fetch_array($result))
  {
  $value = $result;
  }
 
mysql_close($con);
?>

Is this right?

Regardless of what $read is it always seems to echo "Yes"
 

LeCeejay

Member
Dec 25, 2010
122
17
The reason it says "yes" all the time is because you've stated no "else" from what I see, but I may be blind.

However, from my basic knowledge in PHP, I would assume that should work.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Still doesn't seem to be working, I added an elseif and an else, I also tried adding "==" instead of "=".
 

LeCeejay

Member
Dec 25, 2010
122
17
PHP:
<?php[/FONT]
[FONT=Consolas]$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("joshwelford", $con);
 
$result = mysql_query("SELECT * FROM `cms_settings` WHERE `id` = 'site_closed'");
$row = mysql_fetch_row($result);
$read = $row;
if($read = "eefef"){
    echo "YES";
}
while($row = mysql_fetch_array($result))
  {
  $value = $result;
  }
 
mysql_close($con);
?>

Try that, may I ask why you have "$read = "eefef"? Is it not "1" and "0"?[/php][/FONT]
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
PHP:
<?php
 
$mysqli = new mysqli();
$mysqli->connect('localhost', 'root', 'PASSWORD', 'DATABASENAME');
 
if ($result = $mysqli->query("SELECT * FROM `cms_settings`")) {
    while ($row = $result->fetch_object()) {
        if($row->maintenance === "0") {
            echo "NOT IN MAINTENANCE";
            // header('Location: LINK TO PLACE IF MAINTENANCE IS NOT ENABLED');
        }
        else {
            echo "IN MAINTENANCE";
            // header('Location: LINK TO PLACE IF MAINTENANCE IS ENABLED');
        }
    }
}
 
?>

1 = Maintenance Enabled
0 = Maintenance Disabled

This will work but you will need to change your database structure as your database is not set up correctly to do this properly.

Here is what the database should look like :
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I have done exactly what you have done, tried this on multiple web servers including IIS and it still returns "In Maintenance" even when the entry is set to 0.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
What Sean posted should work, if it's not, then there is something wrong on your end.
Sean has posted mysqli when Western is using just mysql.
Western have you changed it to suit your server needs? if you have just copied seans code then it will not work
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Sorry you have to excuse me, I'm not great with MySQL let alone MySQLi... I changed the database etc... and added the details but that's it.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Well I have no idea how to do so, nevermind I'll just scrap it. Thanks for the help. 
Nevermind I got it working!
 

Users who are viewing this thread

Top