Monday, 15 March 2010

PHP Prevent multiple MySQL Connections -



PHP Prevent multiple MySQL Connections -

i have big procedural style php/mysql website, splitted many different files. within each file, include dbconn.php, ensure, db connection available.

it looks 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 phone call several other files via include_once. in total come 30 different files.

every file starts with:

include_once($_server['document_root'].'/dbconn.php');

since i'm using include_once, dbconn.php should not loaded more once, next php warning.

php warning: mysqli_connect(): (hy000/1226): user 'username' has exceeded 'max_user_connections' resource (current value: 20) in /var/www/html/dbconn.php on line 10

my dbconn.php looks 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 double check. if there open mysql connection, file should basicly nothing.

what doing wrong? why php warning , how cut down amount of mysql connections?

basically, it's not different calls single page leading problem. it's fact have multiple open connections @ moment.

make sure close connections after have served purpose.

also, seek avoid kind of construction have, have db related stuff included in 1 file , include 1 time on page.

php mysql mysql-connect php-include

No comments:

Post a Comment