doujuanju3076 2014-06-23 13:32 采纳率: 0%
浏览 73

PHP防止多个MySQL连接

I have a large procedural style php/mysql website, which is splitted into many different files. Within each file, i include my dbconn.php, to ensure, that a db connection is available.

It looks something likes this:

index.php

<?php 
   include_once ('header.php');
   include_once ('nav.php');
   include_once ('content.php');
   include_once ('sidebar.php');
   include_once ('footer.php');
?>

and within each of these files i call several other files via include_once. So in total i come up with about 30 different files.

Every file starts with:

include_once($_SERVER['DOCUMENT_ROOT'].'/dbconn.php');

Since i'm using include_once, dbconn.php should not be loaded more then once, but recently i get following php warning.

PHP Warning:  mysqli_connect(): (HY000/1226): User 'username' has exceeded the 'max_user_connections' resource (current value: 20) in /var/www/html/dbconn.php on line 10

My dbconn.php looks like this:

<?php
   $host = "localhost";
   $user = "username";
   $pass = "password";
   $db = "dbname";

   if(!mysqli_thread_id($con)){
       $con = mysqli_connect($host, $user, $pass, $db);
       mysqli_set_charset($con, 'utf8');
   }
   unset($host,$user,$pass,$db);
?>

So there is a double check. If there already is an open mysql connection, the file should do basicly nothing.

What am i doing wrong? Why do i get this php warning and how do i reduce the amount of mysql connections?

  • 写回答

2条回答 默认 最新

  • dongling2038 2014-06-23 13:35
    关注

    Basically, it's not different calls from a single page that is leading to the problem. It's the fact that you have multiple OPEN connections at the moment.

    Make sure you close you connections after they have served their purpose.

    Also, try to avoid the kind of structure you currently have, have all the DB related stuff included in one file and then include it only once on your page.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大