mysql如果连接错误 - css在下次尝试时显示错误

I have the code below to try 5 times to connect to mysql:

mysqli_report(MYSQLI_REPORT_STRICT);

$NUM_OF_ATTEMPTS = 5;
$attempts = 0;

do {
    try {
        $mysqli = new mysqli("localhost", "root", "", "data");
        break;
    }
    catch (Exception $e) {
        echo mysqli_connect_error();
        unset($mysqli);
        $attempts=$attempts+1;
        sleep(2);
        if($attempts<$NUM_OF_ATTEMPTS){
            continue;
        }
        else{
            exit;
        }
    }
break;
}while($attempts < $NUM_OF_ATTEMPTS);

what happens is, if for example, in the first try some error occur and it connects in the second the CSS will show different. the inputs smaller and things like that. What is wrong?

When it retries, the attempt will always be 0, try to save that in a session variable to make it last over sessions and page reloads...

session_start();    

mysqli_report(MYSQLI_REPORT_STRICT);

$NUM_OF_ATTEMPTS = 5;
$_SESSION["attempts"] = 0;

do {
    try {
        $mysqli = new mysqli("localhost", "root", "", "data");
        break;
    }
    catch (Exception $e) {
        echo mysqli_connect_error();
        unset($mysqli);
        $_SESSION["attempts"] = $_SESSION["attempts"] + 1;
        sleep(2);
        if($_SESSION["attempts"]<$NUM_OF_ATTEMPTS){
            continue;
        }
        else{
            exit;
        }
    }
break;
}while($_SESSION["attempts"] < $NUM_OF_ATTEMPTS);

Your $attempts variable will be again 0 when the page is going to refresh. The best thing you can do 'Olso i use it' is to make a session variable and save the attemps there.

start your session() before the login page and assing the value 0 on $_SESSION['login_attemps'].

session_start();
$_SESSION['login_attemps'] = 0;

on your login page start the session

session_start();

Try to connect to your database and if you don't connect

$_SESSION['login_atemps'] = $_SESSION['login_atemps'] + 1;

then check for the login_atemps

if($_SESSION['login_atemps'] >= 5){
    die("Too many login attemps");
}

Hope it helps