douhu8851 2016-07-30 10:19
浏览 26

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?

  • 写回答

2条回答 默认 最新

  • doudou32012 2016-07-30 10:38
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作