简体   繁体   中英

How to prevent PHP using too many MySQLi connections?

I am creating a messaging website, which uses a MySQL database to store the message data. However, when there are only 5 users online at the same time, it starts failing with " MySQLi: Cannot Connect: Too many MySQL connections ".

I have heard that my host, HelioHost , limits the maximum MySQL connections to four, which explains my error (see this for details).

I want to know how I can change my scripts (which currently connect when the message form is submitted, add it to the database, then disconnect) can only use a maximum of four connections over the server.

Here's some code:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("MySQL server connection failed: " . $conn->connect_error);
}

//Process data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_SESSION["username"];
    $parent = $_POST["thread"];
    $content = test_content($_POST["content"]);

    $sql = "INSERT INTO `threads_replies`(`Parent`, `Author`, `Content`) VALUES ('" . $parent . "','" . $name . "','" . $content . "')";
    $conn->query($sql) or die ("Failed MySQL query to save reply");

    echo "Reply saved. <a href=\"viewthread.php?id=" . $parent . "\"><span class=\"fa fa-chevron-circle-left\"></span> Back to Thread</a>";
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
function test_content($data) {
    $data = str_replace("'", "&#39;", $data);
    return $data;
}

Any ideas?

Right from the docs: http://php.net/manual/en/function.mysqli-connect.php

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    $error = mysqli_connect_error();
    echo "Failed to connect to MySQL: $error";
    echo 'Sorry, pal, 5 already playing around...';    
    exit;// or location('wait.php');
}
# .... 
mysqli_close($link);

For the 5th user - he should reload the page, you can redirect him to something like wait.php :

<!-- htm, head, please add.. -->
<?php 
// no need in PHP, actually 
?>
<h3>Sorry, having too much users, will reload automatically...</h3>  

<script> 
   setTimeout(
     ()=> location.href = 
       location.href + '?t=' + (new Date()).getTime(), 
     5000 // hope 5 seconds will be enough
   );

So your code:

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    location('wait.php');
    die("MySQL server connection failed: " . $conn->connect_error);
}

You can build a cache system which will be checked before accessing the database (and build up a connection). As an example, you don't need to read the news on the front page every time from the database, they don't change that often. So you read the news from the cache system (like some file in the file system) instead and display the data from it. And when the news entries has been changed you delete/invalidate the cache so the cache gets rebuild (once) on the next visit again.

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