database.php
changeset 27 4a9469bada05
child 51 508400fc5282
equal deleted inserted replaced
26:51560f5414a6 27:4a9469bada05
       
     1 <?php
       
     2 
       
     3 $mysql_conn = false;
       
     4 
       
     5 function mysql_reconnect()
       
     6 {
       
     7   global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname;
       
     8   if ( $mysql_conn )
       
     9   {
       
    10     @mysql_close($mysql_conn);
       
    11     if ( defined('LIBIRC_DEBUG') )
       
    12     {
       
    13       echo "< > Reconnecting to MySQL\n";
       
    14     }
       
    15   }
       
    16   // connect to MySQL
       
    17   $mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);
       
    18   if ( !$mysql_conn )
       
    19   {
       
    20     $m_e = mysql_error();
       
    21     echo "Error connecting to MySQL: $m_e\n";
       
    22     exit(1);
       
    23   }
       
    24   $q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn);
       
    25   if ( !$q )
       
    26   {
       
    27     $m_e = mysql_error();
       
    28     echo "Error selecting database: $m_e\n";
       
    29     exit(1);
       
    30   }
       
    31 }
       
    32 
       
    33 function eb_mysql_query($sql, $conn = false)
       
    34 {
       
    35   global $mysql_conn, $irc;
       
    36   $m_et = false;
       
    37   while ( true )
       
    38   {
       
    39     $q = mysql_query($sql, $mysql_conn);
       
    40     if ( !$q )
       
    41     {
       
    42       $m_e = mysql_error();
       
    43       if ( strpos($m_e, 'gone away') && !$m_et )
       
    44       {
       
    45         mysql_reconnect();
       
    46         continue;
       
    47       }
       
    48       $m_et = true;
       
    49       // alert everyone on the bot's alert list
       
    50       if ( is_object($irc) )
       
    51       {
       
    52         global $alert_list;
       
    53         foreach ( $alert_list as $nick )
       
    54         {
       
    55           $irc->privmsg($nick, "MySQL query error: $m_e");
       
    56         }
       
    57       }
       
    58       else
       
    59       {
       
    60         echo "\nQUERY ERROR: $m_e\nQuery: $sql\n";
       
    61         exit(1);
       
    62       }
       
    63       return false;
       
    64     }
       
    65     break;
       
    66   }
       
    67   return $q;
       
    68 }
       
    69 
       
    70 function db_escape($str)
       
    71 {
       
    72   return mysql_real_escape_string($str);
       
    73 }