简体   繁体   中英

Keep getting MySQLi Error

When I'm trying to connect to MySQLi, I keep getting this error:

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

I have no clue why this happens. I've tried everything to fix it.

Can anyone atleast explain to me what this means? This is what is causing the error:

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

For those who keep asking for more code:

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

In those files (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;
    }
  } 
}

?>

And In 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>');
  }
}

?>

global $mysqli; should come "after" $mysqli = new mysqli ...

To explain: Since you're trying to set $mysqli as a global before your connection, will not work because it hasn't been assigned ( $mysqli ) therefore it's outside the variable scope.

Read the PHP manual on the subject for more information on globals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM