简体   繁体   English

如何防止PHP使用过多的MySQLi连接?

[英]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. 我正在创建一个消息传递网站,该网站使用MySQL数据库存储消息数据。 However, when there are only 5 users online at the same time, it starts failing with " MySQLi: Cannot Connect: Too many MySQL connections ". 但是,当同时只有5个用户在线时,它将开始失败,并显示“ 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). 我听说我的主人, HelioHost ,限制最大的MySQL连接到四个,这也解释了我的错误(见这个细节)。

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 从文档开始: 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 : 对于第5位用户-他应该重新加载页面,您可以将他重定向到类似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. 当新闻条目已更改时,您将删除/使缓存无效,以便下次访问时再次重建(一次)缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM