SQL连接会创建与主机的过多并发HTTP连接

时间:2015-01-27 20:25:17

标签: php sql mysqli

我一直在评论我的PHP脚本的部分内容,直到这是我最终的结果。这件事在一分钟内创建了大约200到300个并发连接到SQL ip(从网关检查),我不明白为什么。 不应该关闭SQL连接结束服务器之间的通信吗?

PHP脚本通过JavaScript每秒调用一次,我是网站上唯一的用户。

袜子的PHP实现(取自网络,fclose()添加,因为我读袜子的方式已关闭)

<?php 
$cookie="tD2h6";
$data = $_COOKIE[$cookie];
parse_str($data, $output);
$name = $output['name'];
$pass = $output['pass'];

$con=mysqli_connect("89.33.242.99","global","changeme","global");

$sql = 'SELECT * FROM `users` WHERE `username`=?';

# Prepare statement 
$stmt = $con->prepare($sql);
if($stmt === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $con->errno . ' ' . $con->error, E_USER_ERROR);
}

# Bind parameters. Types: s = string, i = integer, d = double,  b = blob 
$stmt->bind_param('s', $name);

# Execute statement 
$stmt->execute();

$res = $stmt->get_result();
$row = $res->fetch_assoc();

if($row['password']===$pass && !empty($pass))
{
    $hisusername = $name;
    $hiscredits = $row['credits'];
    $hiseuro = $row['euro'];
}
else
{
    $hisusername = "Guest";
    $hiscredits = "0";
    $hiseuro = "0";
}

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM `users`");
$num_rows = mysqli_num_rows($result);

$result = mysqli_query($con,"SELECT * FROM `users` WHERE admlevel>0");
$num_admrows = mysqli_num_rows($result);

$data = array();
$i=1;
$result = mysqli_query($con,"SELECT * FROM jbchat ORDER BY id DESC LIMIT 7");
while($row = mysqli_fetch_array($result)) 
{
    $data[$i] = $row['string'];
    $i=$i+1;
}
for($i=7;$i>0;--$i)
{
    $jbchat = $jbchat . $data[$i] . "<br>";
}

unset($data);
$data = array();
$i=1;
$result = mysqli_query($con,"SELECT * FROM frchat ORDER BY id DESC LIMIT 7");
while($row = mysqli_fetch_array($result)) 
{
    $data[$i] = $row['string'];
    $i=$i+1;
}
for($i=7;$i>0;--$i)
{
    $frchat = $frchat . $data[$i] . "<br>";
}

unset($data);
$data = array();
$i=1;
$result = mysqli_query($con,"SELECT * FROM drchat ORDER BY id DESC LIMIT 7");
while($row = mysqli_fetch_array($result)) 
{
    $data[$i] = $row['string'];
    $i=$i+1;
}
for($i=7;$i>0;--$i)
{
    $drchat = $drchat . $data[$i] . "<br>";
}

unset($data);
$data = array();
$i=1;
$result = mysqli_query($con,"SELECT * FROM cschat ORDER BY id DESC LIMIT 7");
while($row = mysqli_fetch_array($result)) 
{
    $data[$i] = $row['string'];
    $i=$i+1;
}
for($i=7;$i>0;--$i)
{
    $cschat = $cschat . $data[$i] . "<br>";
}

$today = getdate();
$date = $today['mday'] . "/" . $today['mon'] . "/" . $today['year'];
if($today['minutes']>9)
$time = $today['hours'] . ":" . $today['minutes'];
else
$time = $today['hours'] . ":0" . $today['minutes'];

$sqlx = 'SELECT * FROM notifications WHERE username=? ORDER BY id DESC LIMIT 5'; 
# Prepare statement 
$stmt = $con->prepare($sqlx);
if($stmt === false) {
  trigger_error('Wrong SQL: ' . $sqlx . ' Error: ' . $con->errno . ' ' . $con->error, E_USER_ERROR);
}


# Bind parameters. Types: s = string, i = integer, d = double,  b = blob
$stmt->bind_param('s', $name);

$stmt->execute();

$res = $stmt->get_result();
while($row = $res->fetch_assoc()) 
{
    if($row['read']==0)
        $nnumber = $nnumber+1;
    $notifications = $notifications . "
    <li>
    <a href=\"#\" onclick=\"invisphp2('http://r4ge.ro/php/readnotif.php?notifid=" . $row['id'] . "')\">
    <i class=\"fa fa-warning danger\"></i>" . $row['text'] . "
    <br>" . $row['date'] . "
    </a>
    </li>";
}

$result = mysqli_query($con,"SELECT * FROM chat ORDER BY id DESC LIMIT 30");
    $data = array();
    $i=1;
    while($row = mysqli_fetch_array($result)) 
    {
        $data[$i] = $row['name'] . ": " . $row['msg'];
        $i=$i+1;
    }
    for($i=30;$i>0;--$i)
    {
        $lchat = $lchat . $data[$i] . "<br>";
    }


echo json_encode(array(
"registered" => $num_rows,
"admins" => $num_admrows,
"time" => $time,
"date" => $date,
"nnumber" => $nnumber,
"notifications" => $notifications,
"lchat" => $lchat,
"hisusername" => $hisusername,
"hiscredits" => $hiscredits,
"hiseuro" => $hiseuro
));

mysqli_close($con);
?>

编辑:在收听现在删除的评论后,我删除了除第一个以外的每个查询,因此现在正在运行此代码,连接在20-30秒内仍然飙升至150.

<?php 
$cookie="tD2h6";
$data = $_COOKIE[$cookie];
parse_str($data, $output);
$name = $output['name'];
$pass = $output['pass'];

$con=mysqli_connect("89.33.242.99","global","changeme","global");

$sql = 'SELECT * FROM `users` WHERE `username`=?';

# Prepare statement 
$stmt = $con->prepare($sql);
if($stmt === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $con->errno . ' ' . $con->error, E_USER_ERROR);
}

# Bind parameters. Types: s = string, i = integer, d = double,  b = blob 
$stmt->bind_param('s', $name);

# Execute statement 
$stmt->execute();

$res = $stmt->get_result();
$row = $res->fetch_assoc();

if($row['password']===$pass && !empty($pass))
{
    $hisusername = $name;
    $hiscredits = $row['credits'];
    $hiseuro = $row['euro'];
}
else
{
    $hisusername = "Guest";
    $hiscredits = "0";
    $hiseuro = "0";
}

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


echo json_encode(array(
"registered" => $num_rows,
"admins" => $num_admrows,
"time" => $time,
"date" => $date,
"nnumber" => $nnumber,
"notifications" => $notifications,
"lchat" => $lchat,
"hisusername" => $hisusername,
"hiscredits" => $hiscredits,
"hiseuro" => $hiseuro
));

mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

我知道这会让我看起来很糟糕。 不幸的是,这个特定的代码没什么不好的。

网站框架中的问题处于更深层次,上面的代码是主页,这让我觉得它是问题的根源。

对于@developerwjk,答案是否定的,结合过程和面向对象的实现对mysqli的功能没有任何影响,它的效果很好。

罪魁祸首:在每个创建连接的PHP结束时缺少mysqli_close()

当文档说在脚本端关闭连接时,不要信任该文档,只是为了安全起见。