Jump to content

PHP: Persistent Connection in a global variable


CMMDB

Recommended Posts

Hey!

So my Heliohost site is up and running smoothly, but not optimally. As visits increase, I've been getting errors regarding exceeding the number of allowed MySQL connections.So I looked up a snippet and adapted it to my needs. Question is, since I can't really see the amount of current MySQL connections in a way that I'm aware of, would this snippet theoretically do the job?Did I adapt it correctly? I include this wherever I need a db connection(Most every page) and call GetMyConnection().

 

(fatal_error() just dies with an image attached)

<?php
   if (!isset($g_link)) {
       $g_link = false;
   }

   function GetMyConnection()
   {
       global $g_link;
       if( $g_link )
           return $g_link;
       $g_link = @mysql_pconnect( 'localhost:3306', 'usr', 'pwd') or fatal_error("Could not connect to mysql server.<!--".mysql_error()."-->" );
       @mysql_select_db('db', $g_link) or fatal_error("Could not select database.<!--".mysql_error()."-->");
       return $g_link;
   }

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...