2016-07-24 53 views
1

我有這個連接的mysqli:化妝用戶等待嘗試重新連接 - mysqli的PHP

$mysqli_c = new mysqli($host, $us, $password, $bd); 

if (mysqli_connect_errno()) { 
    if (mysqli_errno() == 1203) { 
     header("Location: too_many.php"); 
     exit; 
    } 
} 

這得到了「太多的連接」的錯誤。

我想要的是,而不是重定向到too_many.php,如果錯誤1203發生,我想在10秒後再次嘗試。這樣用戶可以認爲我的網站加載速度很慢,但他不會看到錯誤頁面。

任何ideasis如何做到這一點?可以嗎?

+1

你可以循環,直到它連接但如果數據庫實際上是下降,用戶將只是循環,直到他們關閉網頁.. – chris85

+3

看看這個Q&A http://stackoverflow.com/questions/32031685/php-if-no-results-wait-and-try-again and possibly http://stackoverflow.com/questions/35890629/php-echo-first-row-wait-sleep-then-echo-第二行可以使用'sleep()'函數給你一些想法。 –

+0

@ chris85噢,也許嘗試連接一兩次,然後重定向,如果不可能的話! –

回答

0
<?php 

    function connect($i) { 
     $mysqli_c = new mysqli($host, $us, $password, $bd); 
     if (mysqli_connect_errno()) { 
      if (mysqli_errno() == 1203) { 
       $i=$i+1; 
       if ($i >= 4) { 
        header("Location: too_many.php"); 
        exit; 
       } 
       sleep(10); 
       connect($i); 
      } 
     } 
    } 
    connect(1); 

?> 
+0

你在這裏沒有用'$ i'做任何事情。 (除了增加它) – chris85

+0

@ chris85哦,是的......例如,如果'$ i> = 4',未來的實現將停止嘗試和重定向。 –

+1

我會添加該筆記並刪除問題(「類似這個函數???')。這樣可能對未來的訪問者有用。 – chris85

1
do { 
$status="ok"; 
    $sql = new mysqli("localhost", "root", "", "site"); 
    if (mysqli_connect_errno()) { 
     $status = 'error'; 
     sleep(10); 
    } 
}while($status == 'error'); 
+0

一直在努力與代碼編輯器忘記糾正它, – Amir