连接超时

时间:2017-08-07 12:15:07

标签: php mysql

收到消息  连接超时

是否有另一种方法可以在不加载数据库的情况下获得结果

$B1=$_REQUEST['B1'];
if($B1){
$T1=strtotime($_REQUEST['T1']);
$T2=strtotime($_REQUEST['T2']);
if($B1){
$datach=" `date_time` between '$T1' and '$T2' ";
}else{
$datach=" date_time>=$Timestamp ";
}
$query=mysql_query("select * from euser where `show`='0' order by id desc ");
while ($row=mysql_fetch_array($query)){
$blockid=$row['id'];
$links=mysql_query("select * from rafia_news where usre=$blockid and type=0 and $datach ");
$links2=mysql_query("select * from rafia_news where usre=$blockid and type=1 and $datach ");
$links3=mysql_query("select * from rafia_news where usre=$blockid and type=2 and $datach ");
$count=mysql_num_rows($links);
$count2=mysql_num_rows($links2);
$count3=mysql_num_rows($links3);


    <tr>
    <td><? echo $row['title'];?></td>
    <td width="134"><? echo $count ; ?></td>
    <td width="111"><? echo $count2 ; ?></td>
    <td width="111"><? echo $count3 ; ?></td>
</tr>

1 个答案:

答案 0 :(得分:1)

由于您只需要每种类型的行数,您可以合并这3个查询,如下所示,这将显着减少负载:

SELECT count(usre), type FROM rafia_news WHERE usre=$blockid and type in (0, 1, 2) and $datach GROUP BY type

其次,不要使用已弃用且不安全 mysql * - 函数。它们自PHP 5.5(2013年)以来已被弃用,并在PHP 7(2015年)中被完全删除。请改用MySQLi或PDO。 2.您 SQL Injections 应该使用Prepared Statements而不是连接您的查询,如果您使用上述MySQLi或PDO,则可以使用这些查询。

以下是更新后的代码:

<?php
$B1=$_REQUEST['B1'];
if($B1){
    $T1=strtotime($_REQUEST['T1']);
    $T2=strtotime($_REQUEST['T2']);
    if($B1){
        $datach=" `date_time` between '$T1' and '$T2' ";
    }else{
        $datach=" date_time>=$Timestamp ";
    }
    $query=mysql_query("select * from euser where `show`='0' order by id desc ");
    while ($row=mysql_fetch_array($query)){
        $blockid=$row['id'];
        $countRecords[0] = $countRecords[1] = $countRecords[2] = 0;
        $links=mysql_query("select count(usre), type from rafia_news where usre=$blockid and type in (0, 1, 2) and $datach GROUP BY type");
        while($countRows = mysql_fetch_array($links)) {
            $countRecords[$countRows[1]] = $countRows[0];
        }

        <tr>
            <td><? echo $row['title'];?></td>
<td width="134">
    <? echo $countRecords[0] ; ?></td>
<td width="111">
    <? echo $countRecords[1] ; ?></td>
<td width="111">
    <? echo $countRecords[2] ; ?></td>
</tr>
?>