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