继续获取MySQLi错误

时间:2013-11-26 16:32:10

标签: php mysql mysqli

当我尝试连接MySQLi时,我不断收到此错误:

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/1105): (proxy) all backends are down in /home/xylotk/public_html/Main/Header.php on line 5

Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch mysqli in /home/xylotk/public_html/Main/Logged.php on line 5

Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch mysqli in /home/xylotk/public_html/Main/Logged.php on line 6

Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in /home/xylotk/public_html/Main/Logged.php on line 7

Fatal error: Call to a member function fetch_array() on a non-object in /home/xylotk/public_html/Main/Logged.php on line 8

我不知道为什么会这样。 我已经尝试了一切来解决它。

任何人至少可以向我解释这意味着什么? 这是造成错误的原因:

 global $mysqli;
 $mysqli = new mysqli("localhost", "xylotk_chris", "-hidden-", "xylotk_database");

对于那些不断要求更多代码的人:

 include "Logged.php";
 include "Login.php";

在这些文件中(Logged.php):

<?php

$logged = false;
if($_COOKIE['xy_user'] && $_COOKIE['xy_salt']){
   $xyuser = $mysqli->real_escape_string($_COOKIE['xy_user']);
   $xysalt = $mysqli->real_escape_string($_COOKIE['xy_salt']);
   $usrquery = $mysqli->query("SELECT * FROM `Accounts` WHERE `Salt`='$xysalt'");
   $usr = $usrquery->fetch_array();
   if($usr != 0){
      if(hash("sha512", $usr['Username']) == $xyuser){
         $logged = true;
    }
  } 
}

?>

在Login.php中:

<?php

if($_POST['Login']){
   if($_POST['existUsername'] && $_POST['existPassword']){

      $Username = $mysqli->real_escape_string($_POST['existUsername']);
      $Password = $mysqli->real_escape_string(hash("sha512", $_POST['existPassword']));

      $userquery = $mysqli->query("SELECT * FROM `Accounts` WHERE `Username`='$Username'");
      $user = $userquery->fetch_array();
      if($user == '0'){

            die('<div class="box boxerror">That username does not exist. You can still <a href="/Login/Register.php">register <b>' . $Username . '</b></a>. <a href="/" class="btn btn-danger">Go Back</a></div>');

      }
      if($user['Password'] != $Password){

      die('<div class="box boxerror">The password entered was incorrect. <a href="/" class="btn btn-danger">Go Back</a></div>');
      }      

      $Salt = hash("sha512", rand() . rand() . rand());
      setcookie("xy_user", hash("sha512", $Username), time() + 24 * 60 * 60, "/");
      setcookie("xy_salt", $Salt, time() + 24 * 60 * 60, "/");

      $userID = $user['ID'];     
      $mysqli->query("UPDATE `Accounts` SET `Salt`='$Salt' WHERE `ID`='$userID'") or die($mysqli->error());

      header('Location: /Dashboard');

  }else{
   die('<div class="box boxerror">You missed some fields! <a href="/" class="btn btn-danger">Go Back</a></div>');
  }
}

?>

1 个答案:

答案 0 :(得分:0)

global $mysqli;应该“在$mysqli = new mysqli之后......

解释:由于您尝试在连接之前将$mysqli设置为全局,因此无法正常工作,因为它尚未分配($mysqli),因此它位于变量范围之外。< / p>

阅读有关该主题的PHP手册,了解有关全局变量的更多信息