0

我在我的 index.php 中使用了这个

<?
    include('config.php');
    if($site->maintenance > 0){
        echo "<script>document.location.href='maintenance'</script>";
        exit;
    }
?>

并在我的 config.php 检查数据库连接后

$site = mysql_fetch_object(mysql_query("SELECT * FROM problems"));

我在我的数据库中创建了一个表并调用它,problems但即使我将值设置为 0,它也会将我转移到维护。$site当我通过它检查变量var_dump($site)时输出:

bool(false)

如果我像这样检查它:var_dump($site->maintenance)它输出:

NULL

我必须做些什么来管理数据库的维护以及当我希望我的网站工作时我改变价值?

4

2 回答 2

2

你为什么要为此使用 JS?如果用户 JS 关闭了怎么办?我会改用 PHP

使用列创建一个维护表maintenance_status,现在它将保存一个布尔值,0 1... 0 => off,1 => on,将只保留一条记录,该记录将被创建一次,稍后将更新它总是...

所以现在稍后你创建这个函数

function check_maintenance($connection) { /* Call this function on every page, 
                                          pass your database connection var 
                                          as a function parameter */
   $query = mysqli_fetch_array(mysqli_query($connection, "SELECT * FROM tbl_maintenance LIMIT 1")); 
   /* Limit 1 is optional if you are using only 1 row as I told you, 
      if you are keeping records of the previous maintenance, probably 
      you've to sort desc and use limit 1 */

   if($query['tbl_maintenance'] == '1') { //Check boolean value, if it's on than redirect
      header('Location: maintenance.php'); //Redirect the user to maintenance page
      exit;
   }
}
于 2013-05-04T11:40:50.587 回答
0

事实上,可能$sitefalse由查询问题引起的。将mysql代码更改为:

$result = mysql_query("SELECT * FROM problems");
if(!$result) {
    die(mysql_error();
}

$site = mysql_fetch_object($result);

此外,您应该学习如何在 PHP 中启用错误消息。我猜他们有很多。默认情况下禁用它们,因为这可能是生产系统中的安全风险。但是当你在开发时,你必须启用它们。您可以在php.ini开发系统中启用它:

php.ini

...
display_errors=1
...
log_errors=1
...
error_log="/path/to/writable/file"
...
error_reporting=E_ALL

修改后php.ini不要忘记重启网络服务器。

于 2013-05-04T11:42:39.670 回答