1

I have this to connect to mysqli:

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

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

this gets the "too many connections" error.

what I want is instead of redirect to too_many.php, if error 1203 occur, I want to try again after 10 seconds. This way user can think my website is slow to load, but he will not see the error page.

any ideais how to do this? is it ok?

RGS
  • 4,062
  • 4
  • 31
  • 67
  • 1
    You could loop it until it connects but what if the DB actually is down, users will just loop until they close the page.. – chris85 Jul 24 '16 at 15:28
  • 3
    have a look at this 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-second-row which could give you some ideas using the `sleep()` function. – Funk Forty Niner Jul 24 '16 at 15:29
  • @chris85 oh yeah, maybe try to connect one or two times then redirect if not possible! – RGS Jul 24 '16 at 15:30
  • 2
    @Fred-ii- `sleep()` is what I need, thank you! – RGS Jul 24 '16 at 15:33
  • 2
    You're welcome Rick. – Funk Forty Niner Jul 24 '16 at 15:33

2 Answers2

1
do {
$status="ok";
    $sql = new mysqli("localhost", "root", "", "site");
    if (mysqli_connect_errno()) {
        $status = 'error';
        sleep(10);
    }
}while($status == 'error');
RGS
  • 4,062
  • 4
  • 31
  • 67
Amir
  • 194
  • 1
  • 7
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);

?>
RGS
  • 4,062
  • 4
  • 31
  • 67
  • You're not doing anything with `$i` here. (while aside from incrementing it) – chris85 Jul 24 '16 at 16:24
  • @chris85 oh, yeah... it is for a future implementation to stop trying and redirect if `$i >= 4`, for example. – RGS Jul 24 '16 at 16:27
  • 1
    I'd add that note in and remove the question (`something like this function???`). That way this could be useful for future visitors. – chris85 Jul 24 '16 at 16:28