Index: /trunk/action.php
===================================================================
--- /trunk/action.php	(revision 4324)
+++ /trunk/action.php	(revision 4325)
@@ -72,5 +72,5 @@
 
 $result = pwg_query($query);
-$element_info = mysql_fetch_assoc($result);
+$element_info = pwg_db_fetch_assoc($result);
 if ( empty($element_info) )
 {
@@ -94,5 +94,5 @@
   LIMIT 1
 ;';
-if ( mysql_num_rows(pwg_query($query))<1 )
+if ( pwg_db_num_rows(pwg_query($query))<1 )
 {
   do_error(401, 'Access denied');
Index: /trunk/admin/cat_list.php
===================================================================
--- /trunk/admin/cat_list.php	(revision 4324)
+++ /trunk/admin/cat_list.php	(revision 4325)
@@ -120,5 +120,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $categories[ $row['id'] ] = strtolower($row['name']);
Index: /trunk/admin/cat_modify.php
===================================================================
--- /trunk/admin/cat_modify.php	(revision 4324)
+++ /trunk/admin/cat_modify.php	(revision 4325)
@@ -194,5 +194,5 @@
   WHERE id = '.$_GET['cat_id'].'
 ;';
-$category = mysql_fetch_assoc( pwg_query( $query ) );
+$category = pwg_db_fetch_assoc( pwg_query( $query ) );
 // nullable fields
 foreach (array('comment','dir','site_id', 'id_uppercat') as $nullable)
@@ -211,5 +211,5 @@
   LIMIT 1';
 $result = pwg_query($query);
-$category['has_images'] = mysql_num_rows($result)>0 ? true : false;
+$category['has_images'] = pwg_db_num_rows($result)>0 ? true : false;
 
 // Navigation path
@@ -374,5 +374,5 @@
   WHERE id = '.$category['representative_picture_id'].'
 ;';
-    $row = mysql_fetch_assoc(pwg_query($query));
+    $row = pwg_db_fetch_assoc(pwg_query($query));
     $src = get_thumbnail_url($row);
     $url = get_root_url().'admin.php?page=picture_modify';
@@ -462,7 +462,7 @@
 
     $result = pwg_query($query);
-    if (mysql_num_rows($result) > 0)
+    if (pwg_db_num_rows($result) > 0)
     {
-      $element = mysql_fetch_assoc($result);
+      $element = pwg_db_fetch_assoc($result);
 
       $img_url  = '<a href="'.
@@ -512,5 +512,5 @@
   WHERE id = '.$_POST['group'].'
 ;';
-  list($group_name) = mysql_fetch_row(pwg_query($query));
+  list($group_name) = pwg_db_fetch_row(pwg_query($query));
   
   array_push(
Index: /trunk/admin/cat_perm.php
===================================================================
--- /trunk/admin/cat_perm.php	(revision 4324)
+++ /trunk/admin/cat_perm.php	(revision 4325)
@@ -46,5 +46,5 @@
   WHERE id = '.$_GET['cat'].'
 ;';
-  list($status) = mysql_fetch_row(pwg_query($query));
+  list($status) = pwg_db_fetch_row(pwg_query($query));
   
   if ('private' == $status)
@@ -63,5 +63,5 @@
 ;';
 
-  list($page['cat']) = mysql_fetch_row(pwg_query($query));
+  list($page['cat']) = pwg_db_fetch_row(pwg_query($query));
 }
 
@@ -111,5 +111,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($granteds[$row['cat_id']], $row['group_id']);
@@ -170,5 +170,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($granteds[$row['cat_id']], $row['user_id']);
@@ -275,5 +275,5 @@
 ';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     if (!isset($granted_groups[$row['group_id']]))
Index: /trunk/admin/comments.php
===================================================================
--- /trunk/admin/comments.php	(revision 4324)
+++ /trunk/admin/comments.php	(revision 4325)
@@ -146,5 +146,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $thumb = get_thumbnail_url(
Index: /trunk/admin/configuration.php
===================================================================
--- /trunk/admin/configuration.php	(revision 4324)
+++ /trunk/admin/configuration.php	(revision 4325)
@@ -139,5 +139,5 @@
     //echo '<pre>'; print_r($_POST); echo '</pre>';
     $result = pwg_query('SELECT param FROM '.CONFIG_TABLE);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       if (isset($_POST[$row['param']]))
Index: /trunk/admin/element_set.php
===================================================================
--- /trunk/admin/element_set.php	(revision 4324)
+++ /trunk/admin/element_set.php	(revision 4325)
@@ -193,5 +193,5 @@
   $query = 'SELECT MAX(date_available) AS date
   FROM '.IMAGES_TABLE;
-  if ($row = mysql_fetch_assoc( pwg_query($query) ) )
+  if ($row = pwg_db_fetch_assoc( pwg_query($query) ) )
   {
     $query = 'SELECT id
Index: /trunk/admin/element_set_global.php
===================================================================
--- /trunk/admin/element_set_global.php	(revision 4324)
+++ /trunk/admin/element_set_global.php	(revision 4325)
@@ -246,5 +246,5 @@
     $result = pwg_query($query);
 
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $data = array();
@@ -338,5 +338,5 @@
     AND storage_category_id IS NULL
 ;';
-  list($counter) = mysql_fetch_row(pwg_query($query));
+  list($counter) = pwg_db_fetch_row(pwg_query($query));
 
   if ($counter > 0)
@@ -482,5 +482,5 @@
   // template thumbnail initialization
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $src = get_thumbnail_url($row);
Index: /trunk/admin/element_set_ranks.php
===================================================================
--- /trunk/admin/element_set_ranks.php	(revision 4324)
+++ /trunk/admin/element_set_ranks.php	(revision 4325)
@@ -116,5 +116,5 @@
   WHERE id = '.$page['category_id'].'
 ;';
-$category = mysql_fetch_assoc(pwg_query($query));
+$category = pwg_db_fetch_assoc(pwg_query($query));
 
 // Navigation path
@@ -151,5 +151,5 @@
 $current_rank = 1;
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $src = get_thumbnail_url($row);
Index: /trunk/admin/element_set_unit.php
===================================================================
--- /trunk/admin/element_set_unit.php	(revision 4324)
+++ /trunk/admin/element_set_unit.php	(revision 4325)
@@ -57,5 +57,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $data = array();
@@ -201,5 +201,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     // echo '<pre>'; print_r($row); echo '</pre>';
Index: /trunk/admin/group_list.php
===================================================================
--- /trunk/admin/group_list.php	(revision 4324)
+++ /trunk/admin/group_list.php	(revision 4325)
@@ -61,5 +61,5 @@
   WHERE id = '.$_GET['delete'].'
 ;';
-  list($groupname) = mysql_fetch_row(pwg_query($query));
+  list($groupname) = pwg_db_fetch_row(pwg_query($query));
   
   // destruction of the group
@@ -95,5 +95,5 @@
   WHERE name = \''.$_POST['groupname'].'\'
 ;';
-    list($count) = mysql_fetch_row(pwg_query($query));
+    list($count) = pwg_db_fetch_row(pwg_query($query));
     if ($count != 0)
     {
@@ -108,5 +108,5 @@
   (name)
   VALUES
-  (\''.mysql_real_escape_string($_POST['groupname']).'\')
+  (\''.pwg_db_real_escape_string($_POST['groupname']).'\')
 ;';
     pwg_query($query);
@@ -130,5 +130,5 @@
   WHERE id = '.$_GET['toggle_is_default'].'
 ;';
-  list($groupname, $is_default) = mysql_fetch_row(pwg_query($query));
+  list($groupname, $is_default) = pwg_db_fetch_row(pwg_query($query));
   
   // update of the group
@@ -176,5 +176,5 @@
 $toggle_is_default_url     = $admin_url.'group_list&amp;toggle_is_default=';
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $query = '
@@ -183,5 +183,5 @@
   WHERE group_id = '.$row['id'].'
 ;';
-  list($counter) = mysql_fetch_row(pwg_query($query));
+  list($counter) = pwg_db_fetch_row(pwg_query($query));
   
   $template->append(
Index: /trunk/admin/group_perm.php
===================================================================
--- /trunk/admin/group_perm.php	(revision 4324)
+++ /trunk/admin/group_perm.php	(revision 4325)
@@ -80,5 +80,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($private_uppercats, $row['id']);
@@ -97,5 +97,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($authorized_ids, $row['cat_id']);
@@ -158,5 +158,5 @@
 $result = pwg_query($query_true);
 $authorized_ids = array();
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($authorized_ids, $row['id']);
Index: /trunk/admin/history.php
===================================================================
--- /trunk/admin/history.php	(revision 4324)
+++ /trunk/admin/history.php	(revision 4325)
@@ -111,5 +111,5 @@
       '*',
       '%',
-      mysql_real_escape_string($_POST['filename'])
+      pwg_db_real_escape_string($_POST['filename'])
       );
   }
@@ -137,5 +137,5 @@
     pwg_query($query);
 
-    $search_id = mysql_insert_id();
+    $search_id = pwg_db_insert_id();
 
     redirect(
@@ -178,5 +178,5 @@
   WHERE id = '.$page['search_id'].'
 ;';
-  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
+  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
 
   $page['search'] = unserialize($serialized_rules);
@@ -199,5 +199,5 @@
     pwg_query($query);
 
-    $search_id = mysql_insert_id();
+    $search_id = pwg_db_insert_id();
 
     redirect(
@@ -258,5 +258,5 @@
 
     $username_of = array();
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $username_of[$row['id']] = stripslashes($row['username']);
@@ -306,5 +306,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $label_of_image[ $row['id'] ] = $row['label'];
@@ -342,5 +342,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $name_of_tag[ $row['id'] ] = $row['name'];
Index: /trunk/admin/include/c13y_internal.class.php
===================================================================
--- /trunk/admin/include/c13y_internal.class.php	(revision 4324)
+++ /trunk/admin/include/c13y_internal.class.php	(revision 4325)
@@ -43,6 +43,6 @@
     $check_list[] = array('type' => 'PHP', 'current' => phpversion(), 'required' => REQUIRED_PHP_VERSION);
 
-    list($mysql_version) = mysql_fetch_row(pwg_query('SELECT VERSION();'));
-    $check_list[] = array('type' => 'MySQL', 'current' => $mysql_version, 'required' => REQUIRED_MYSQL_VERSION);
+    $db_version = pwg_get_db_version();
+    $check_list[] = array('type' => 'MySQL', 'current' => $db_version, 'required' => REQUIRED_MYSQL_VERSION);
 
     foreach ($check_list as $elem)
@@ -127,5 +127,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $status[$row['id']] = $row['status'];
Index: /trunk/admin/include/functions.php
===================================================================
--- /trunk/admin/include/functions.php	(revision 4324)
+++ /trunk/admin/include/functions.php	(revision 4325)
@@ -37,5 +37,5 @@
   $result = pwg_query($query);
   $category_ids = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($category_ids, $row['id']);
@@ -78,5 +78,5 @@
   $result = pwg_query($query);
   $element_ids = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($element_ids, $row['id']);
@@ -160,5 +160,5 @@
 ;';
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $file_path = $row['path'];
@@ -410,208 +410,4 @@
 
 /**
- * inserts multiple lines in a table
- *
- * @param string table_name
- * @param array dbfields
- * @param array inserts
- * @return void
- */
-function mass_inserts($table_name, $dbfields, $datas)
-{
-  if (count($datas) != 0)
-  {
-    $first = true;
-
-    $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
-    list(, $packet_size) = mysql_fetch_row(pwg_query($query));
-    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
-    $query = '';
-
-    foreach ($datas as $insert)
-    {
-      if (strlen($query) >= $packet_size)
-      {
-        pwg_query($query);
-        $first = true;
-      }
-
-      if ($first)
-      {
-        $query = '
-INSERT INTO '.$table_name.'
-  ('.implode(',', $dbfields).')
-  VALUES';
-        $first = false;
-      }
-      else
-      {
-        $query .= '
-  , ';
-      }
-
-      $query .= '(';
-      foreach ($dbfields as $field_id => $dbfield)
-      {
-        if ($field_id > 0)
-        {
-          $query .= ',';
-        }
-
-        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
-        {
-          $query .= 'NULL';
-        }
-        else
-        {
-          $query .= "'".$insert[$dbfield]."'";
-        }
-      }
-      $query .= ')';
-    }
-    pwg_query($query);
-  }
-}
-
-define('MASS_UPDATES_SKIP_EMPTY', 1);
-/**
- * updates multiple lines in a table
- *
- * @param string table_name
- * @param array dbfields
- * @param array datas
- * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
- * @return void
- */
-function mass_updates($tablename, $dbfields, $datas, $flags=0)
-{
-  if (count($datas) == 0)
-    return;
-  // depending on the MySQL version, we use the multi table update or N update queries
-  if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0)
-  { // MySQL is prior to version 4.0.4, multi table update feature is not available
-    foreach ($datas as $data)
-    {
-      $query = '
-UPDATE '.$tablename.'
-  SET ';
-      $is_first = true;
-      foreach ($dbfields['update'] as $key)
-      {
-        $separator = $is_first ? '' : ",\n    ";
-
-        if (isset($data[$key]) and $data[$key] != '')
-        {
-          $query.= $separator.$key.' = \''.$data[$key].'\'';
-        }
-        else
-        {
-          if ($flags & MASS_UPDATES_SKIP_EMPTY )
-            continue; // next field
-          $query.= "$separator$key = NULL";
-        }
-        $is_first = false;
-      }
-      if (!$is_first)
-      {// only if one field at least updated
-        $query.= '
-  WHERE ';
-        $is_first = true;
-        foreach ($dbfields['primary'] as $key)
-        {
-          if (!$is_first)
-          {
-            $query.= ' AND ';
-          }
-          if ( isset($data[$key]) )
-          {
-            $query.= $key.' = \''.$data[$key].'\'';
-          }
-          else
-          {
-            $query.= $key.' IS NULL';
-          }
-          $is_first = false;
-        }
-        pwg_query($query);
-      }
-    } // foreach update
-  } // if mysql_ver or count<X
-  else
-  {
-    // creation of the temporary table
-    $query = '
-SHOW FULL COLUMNS FROM '.$tablename;
-    $result = pwg_query($query);
-    $columns = array();
-    $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
-    while ($row = mysql_fetch_assoc($result))
-    {
-      if (in_array($row['Field'], $all_fields))
-      {
-        $column = $row['Field'];
-        $column.= ' '.$row['Type'];
-
-        $nullable = true;
-        if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO')
-        {
-          $column.= ' NOT NULL';
-          $nullable = false;
-        }
-        if (isset($row['Default']))
-        {
-          $column.= " default '".$row['Default']."'";
-        }
-        elseif ($nullable)
-        {
-          $column.= " default NULL";
-        }
-        if (isset($row['Collation']) and $row['Collation'] != 'NULL')
-        {
-          $column.= " collate '".$row['Collation']."'";
-        }
-        array_push($columns, $column);
-      }
-    }
-
-    $temporary_tablename = $tablename.'_'.micro_seconds();
-
-    $query = '
-CREATE TABLE '.$temporary_tablename.'
-(
-  '.implode(",\n  ", $columns).',
-  UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).')
-)';
-
-    pwg_query($query);
-    mass_inserts($temporary_tablename, $all_fields, $datas);
-    if ( $flags & MASS_UPDATES_SKIP_EMPTY )
-      $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";');
-    else
-      $func_set = create_function('$s', 'return "t1.$s = t2.$s";');
-
-    // update of images table by joining with temporary table
-    $query = '
-UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2
-  SET '.
-      implode(
-        "\n    , ",
-        array_map($func_set,$dbfields['update'])
-        ).'
-  WHERE '.
-      implode(
-        "\n    AND ",
-        array_map(
-          create_function('$s', 'return "t1.$s = t2.$s";'),
-          $dbfields['primary']
-          )
-        );
-    pwg_query($query);
-    $query = '
-DROP TABLE '.$temporary_tablename;
-    pwg_query($query);
-  }
-}
-
-/**
  * order categories (update categories.rank and global_rank database fields)
  * so that rank field are consecutive integers starting at 1 for each child
@@ -631,5 +427,5 @@
 
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     if ($row['id_uppercat'] != $current_uppercat)
@@ -777,5 +573,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $uppercats = array_merge($uppercats,
@@ -804,5 +600,5 @@
   LIMIT 0,1
 ;';
-    list($representative) = mysql_fetch_row(pwg_query($query));
+    list($representative) = pwg_db_fetch_row(pwg_query($query));
 
     array_push(
@@ -864,5 +660,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($categories, $row);
@@ -1140,5 +936,5 @@
   $datas = array();
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push(
@@ -1207,5 +1003,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $categories[$row['id']] =
@@ -1226,5 +1022,5 @@
   WHERE id = '.$new_parent.'
 ;';
-    list($new_parent_uppercats) = mysql_fetch_row(pwg_query($query));
+    list($new_parent_uppercats) = pwg_db_fetch_row(pwg_query($query));
 
     foreach ($categories as $category)
@@ -1271,5 +1067,5 @@
   WHERE id = '.$new_parent.'
 ;';
-    list($parent_status) = mysql_fetch_row(pwg_query($query));
+    list($parent_status) = pwg_db_fetch_row(pwg_query($query));
   }
 
@@ -1356,5 +1152,5 @@
   WHERE id_uppercat '.(is_numeric($parent_id) ? '= '.$parent_id : 'IS NULL').'
 ;';
-  list($current_rank) = mysql_fetch_row(pwg_query($query));
+  list($current_rank) = pwg_db_fetch_row(pwg_query($query));
 
   $insert = array(
@@ -1372,5 +1168,5 @@
   WHERE id = '.$parent_id.'
 ;';
-    $parent = mysql_fetch_assoc(pwg_query($query));
+    $parent = pwg_db_fetch_assoc(pwg_query($query));
 
     $insert['id_uppercat'] = $parent['id'];
@@ -1418,5 +1214,5 @@
     );
 
-  $inserted_id = mysql_insert_id();
+  $inserted_id = pwg_db_insert_id();
 
   $query = '
@@ -1551,5 +1347,5 @@
       );
 
-    $page['tag_id_from_tag_name_cache'][$tag_name] = mysql_insert_id();
+    $page['tag_id_from_tag_name_cache'][$tag_name] = pwg_db_insert_id();
   }
   else
@@ -1593,68 +1389,4 @@
       $inserts
       );
-  }
-}
-
-/**
- * Do maintenance on all PWG tables
- *
- * @return none
- */
-function do_maintenance_all_tables()
-{
-  global $prefixeTable, $page;
-
-  $all_tables = array();
-
-  // List all tables
-  $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\'';
-  $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
-  {
-    array_push($all_tables, $row[0]);
-  }
-
-  // Repair all tables
-  $query = 'REPAIR TABLE '.implode(', ', $all_tables);
-  $mysql_rc = pwg_query($query);
-
-  // Re-Order all tables
-  foreach ($all_tables as $table_name)
-  {
-    $all_primary_key = array();
-
-    $query = 'DESC '.$table_name.';';
-    $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
-    {
-      if ($row['Key'] == 'PRI')
-      {
-        array_push($all_primary_key, $row['Field']);
-      }
-    }
-
-    if (count($all_primary_key) != 0)
-    {
-      $query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';';
-      $mysql_rc = $mysql_rc && pwg_query($query);
-    }
-  }
-
-  // Optimize all tables
-  $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables);
-  $mysql_rc = $mysql_rc && pwg_query($query);
-  if ($mysql_rc)
-  {
-    array_push(
-          $page['infos'],
-          l10n('Optimizations completed')
-          );
-  }
-  else
-  {
-    array_push(
-          $page['errors'],
-          l10n('Optimizations errors')
-          );
   }
 }
@@ -1890,5 +1622,5 @@
       );
 
-    $inserted_id = mysql_insert_id();
+    $inserted_id = pwg_db_insert_id();
 
     return array(
@@ -2076,7 +1808,7 @@
 ;';
   $result = pwg_query($query);
-  if (mysql_num_rows($result) > 0)
-  {
-    list($groupname) = mysql_fetch_row($result);
+  if (pwg_db_num_rows($result) > 0)
+  {
+    list($groupname) = pwg_db_fetch_row($result);
   }
   else
@@ -2104,7 +1836,7 @@
 ;';
   $result = pwg_query($query);
-  if (mysql_num_rows($result) > 0)
-  {
-    list($username) = mysql_fetch_row($result);
+  if (pwg_db_num_rows($result) > 0)
+  {
+    list($username) = pwg_db_fetch_row($result);
   }
   else
Index: /trunk/admin/include/functions_history.inc.php
===================================================================
--- /trunk/admin/include/functions_history.inc.php	(revision 4324)
+++ /trunk/admin/include/functions_history.inc.php	(revision 4325)
@@ -166,5 +166,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($data, $row);
Index: /trunk/admin/include/functions_metadata.php
===================================================================
--- /trunk/admin/include/functions_metadata.php	(revision 4324)
+++ /trunk/admin/include/functions_metadata.php	(revision 4325)
@@ -260,5 +260,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($cat_ids, $row['id']);
@@ -285,5 +285,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $files[$row['id']] = $row['path'];
Index: /trunk/admin/include/functions_notification_by_mail.inc.php
===================================================================
--- /trunk/admin/include/functions_notification_by_mail.inc.php	(revision 4324)
+++ /trunk/admin/include/functions_notification_by_mail.inc.php	(revision 4325)
@@ -60,5 +60,5 @@
   check_key = \''.$key.'\';';
 
-    list($count) = mysql_fetch_row(pwg_query($query));
+    list($count) = pwg_db_fetch_row(pwg_query($query));
     if ($count == 0)
     {
@@ -168,5 +168,5 @@
     if (!empty($result))
     {
-      while ($nbm_user = mysql_fetch_assoc($result))
+      while ($nbm_user = pwg_db_fetch_assoc($result))
       {
         array_push($data_users, $nbm_user);
Index: /trunk/admin/include/functions_permalinks.php
===================================================================
--- /trunk/admin/include/functions_permalinks.php	(revision 4324)
+++ /trunk/admin/include/functions_permalinks.php	(revision 4325)
@@ -52,6 +52,6 @@
   $result = pwg_query($query);
   $cat_id = null;
-  if ( mysql_num_rows($result) )
-    list( $cat_id ) = mysql_fetch_row($result);
+  if ( pwg_db_num_rows($result) )
+    list( $cat_id ) = pwg_db_fetch_row($result);
   return $cat_id;
 }
@@ -73,7 +73,7 @@
 ;';
   $result = pwg_query($query);
-  if ( mysql_num_rows($result) )
-  {
-    list($permalink) = mysql_fetch_row($result);
+  if ( pwg_db_num_rows($result) )
+  {
+    list($permalink) = pwg_db_fetch_row($result);
   }
   if ( !isset($permalink) )
Index: /trunk/admin/include/functions_upgrade.php
===================================================================
--- /trunk/admin/include/functions_upgrade.php	(revision 4324)
+++ /trunk/admin/include/functions_upgrade.php	(revision 4325)
@@ -113,5 +113,5 @@
   $result = pwg_query($query);
   $plugins = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($plugins, $row['id']);
@@ -139,5 +139,5 @@
   if(!@get_magic_quotes_gpc())
   {
-    $username = mysql_real_escape_string($username);
+    $username = pwg_db_real_escape_string($username);
   }
 
@@ -166,5 +166,5 @@
 ;';
   }
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
 
   if (!isset($conf['pass_convert']))
Index: /trunk/admin/intro.php
===================================================================
--- /trunk/admin/intro.php	(revision 4324)
+++ /trunk/admin/intro.php	(revision 4325)
@@ -54,7 +54,7 @@
     // if the current version is a BSF (development branch) build, we check
     // the first line, for stable versions, we check the second line
-    if (preg_match('/^BSF/', $versions{'current'}))
-    {
-      $versions{'latest'} = trim($lines[0]);
+    if (preg_match('/^BSF/', $versions['current']))
+    {
+      $versions['latest'] = trim($lines[0]);
 
       // because integer are limited to 4,294,967,296 we need to split BSF
@@ -62,5 +62,5 @@
       foreach ($versions as $key => $value)
       {
-        $versions{$key} =
+        $versions[$key] =
           preg_replace('/BSF_(\d{8})(\d{4})/', '$1.$2', $value);
       }
@@ -68,8 +68,8 @@
     else
     {
-      $versions{'latest'} = trim($lines[1]);
-    }
-
-    if ('' == $versions{'latest'})
+      $versions['latest'] = trim($lines[1]);
+    }
+
+    if ('' == $versions['latest'])
     {
       array_push(
@@ -80,5 +80,5 @@
     // concatenation needed to avoid automatic transformation by release
     // script generator
-    else if ('%'.'PWGVERSION'.'%' == $versions{'current'})
+    else if ('%'.'PWGVERSION'.'%' == $versions['current'])
     {
       array_push(
@@ -87,5 +87,5 @@
         );
     }
-    else if (version_compare($versions{'current'}, $versions{'latest'}) < 0)
+    else if (version_compare($versions['current'], $versions['latest']) < 0)
     {
       array_push(
@@ -126,5 +126,6 @@
 
 $php_current_timestamp = date("Y-m-d H:i:s");
-list($mysql_version, $db_current_timestamp) = mysql_fetch_row(pwg_query('SELECT VERSION(), CURRENT_TIMESTAMP;'));
+$db_version = pwg_get_db_version();
+list($db_current_timestamp) = pwg_db_fetch_row(pwg_query('SELECT CURRENT_TIMESTAMP;'));
 
 $query = '
@@ -132,5 +133,5 @@
   FROM '.IMAGES_TABLE.'
 ;';
-list($nb_elements) = mysql_fetch_row(pwg_query($query));
+list($nb_elements) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -138,5 +139,5 @@
   FROM '.CATEGORIES_TABLE.'
 ;';
-list($nb_categories) = mysql_fetch_row(pwg_query($query));
+list($nb_categories) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -145,5 +146,5 @@
   WHERE dir IS NULL
 ;';
-list($nb_virtual) = mysql_fetch_row(pwg_query($query));
+list($nb_virtual) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -152,5 +153,5 @@
   WHERE dir IS NOT NULL
 ;';
-list($nb_physical) = mysql_fetch_row(pwg_query($query));
+list($nb_physical) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -158,5 +159,5 @@
   FROM '.IMAGE_CATEGORY_TABLE.'
 ;';
-list($nb_image_category) = mysql_fetch_row(pwg_query($query));
+list($nb_image_category) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -164,5 +165,5 @@
   FROM '.TAGS_TABLE.'
 ;';
-list($nb_tags) = mysql_fetch_row(pwg_query($query));
+list($nb_tags) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -170,5 +171,5 @@
   FROM '.IMAGE_TAG_TABLE.'
 ;';
-list($nb_image_tag) = mysql_fetch_row(pwg_query($query));
+list($nb_image_tag) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -176,5 +177,5 @@
   FROM '.USERS_TABLE.'
 ;';
-list($nb_users) = mysql_fetch_row(pwg_query($query));
+list($nb_users) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -182,5 +183,5 @@
   FROM '.GROUPS_TABLE.'
 ;';
-list($nb_groups) = mysql_fetch_row(pwg_query($query));
+list($nb_groups) = pwg_db_fetch_row(pwg_query($query));
 
 $query = '
@@ -188,5 +189,5 @@
   FROM '.COMMENTS_TABLE.'
 ;';
-list($nb_comments) = mysql_fetch_row(pwg_query($query));
+list($nb_comments) = pwg_db_fetch_row(pwg_query($query));
 
 $template->assign(
@@ -196,5 +197,6 @@
     'OS' => PHP_OS,
     'PHP_VERSION' => phpversion(),
-    'MYSQL_VERSION' => $mysql_version,
+    'DB_ENGINE' => 'MySQL',
+    'DB_VERSION' => $db_version,
     'DB_ELEMENTS' => l10n_dec('%d element', '%d elements', $nb_elements),
     'DB_CATEGORIES' =>
@@ -224,5 +226,5 @@
   FROM '.IMAGES_TABLE.'
 ;';
-  list($first_date) = mysql_fetch_row(pwg_query($query));
+  list($first_date) = pwg_db_fetch_row(pwg_query($query));
 
   $template->assign(
@@ -244,5 +246,5 @@
   WHERE validated=\'false\'
 ;';
-list($nb_waiting) = mysql_fetch_row(pwg_query($query));
+list($nb_waiting) = pwg_db_fetch_row(pwg_query($query));
 
 if ($nb_waiting > 0)
@@ -263,5 +265,5 @@
   WHERE validated=\'false\'
 ;';
-list($nb_comments) = mysql_fetch_row(pwg_query($query));
+list($nb_comments) = pwg_db_fetch_row(pwg_query($query));
 
 if ($nb_comments > 0)
Index: /trunk/admin/notification_by_mail.php
===================================================================
--- /trunk/admin/notification_by_mail.php	(revision 4324)
+++ /trunk/admin/notification_by_mail.php	(revision 4325)
@@ -146,10 +146,10 @@
   $result = pwg_query($query);
 
-  if (mysql_num_rows($result) > 0)
+  if (pwg_db_num_rows($result) > 0)
   {
     $inserts = array();
     $check_key_list = array();
 
-    while ($nbm_user = mysql_fetch_assoc($result))
+    while ($nbm_user = pwg_db_fetch_assoc($result))
     {
       // Calculate key
@@ -239,5 +239,5 @@
   if (in_array($action, array('list_to_send', 'send')))
   {
-    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
     $is_action_send = ($action == 'send');
@@ -513,5 +513,5 @@
       // Update param
       $result = pwg_query('select param, value from '.CONFIG_TABLE.' where param like \'nbm\\_%\'');
-      while ($nbm_user = mysql_fetch_assoc($result))
+      while ($nbm_user = pwg_db_fetch_assoc($result))
       {
         if (isset($_POST[$nbm_user['param']]))
Index: /trunk/admin/permalinks.php
===================================================================
--- /trunk/admin/permalinks.php	(revision 4324)
+++ /trunk/admin/permalinks.php	(revision 4325)
@@ -100,5 +100,5 @@
   LIMIT 1';
   pwg_query($query);
-  if (mysql_affected_rows()==0)
+  if (pwg_db_affected_rows()==0)
     array_push($page['errors'], 'Cannot delete the old permalink !');
 }
@@ -135,5 +135,5 @@
 $categories=array();
 $result=pwg_query($query);
-while ( $row=mysql_fetch_assoc($result) )
+while ( $row = pwg_db_fetch_assoc($result) )
 {
   $row['name'] = get_cat_display_name_cache( $row['uppercats'] );
@@ -163,5 +163,5 @@
 $result = pwg_query($query);
 $deleted_permalinks=array();
-while ( $row=mysql_fetch_assoc($result) )
+while ( $row = pwg_db_fetch_assoc($result) )
 {
   $row['name'] = get_cat_display_name_cache($row['cat_id']);
Index: /trunk/admin/picture_modify.php
===================================================================
--- /trunk/admin/picture_modify.php	(revision 4324)
+++ /trunk/admin/picture_modify.php	(revision 4325)
@@ -45,5 +45,5 @@
   WHERE id = '.$_GET['image_id'].'
 ;';
-  list($path) = mysql_fetch_row(pwg_query($query));
+  list($path) = pwg_db_fetch_row(pwg_query($query));
   update_metadata(array($_GET['image_id'] => $path));
 
@@ -176,5 +176,5 @@
   WHERE id = '.$_GET['image_id'].'
 ;';
-$row = mysql_fetch_assoc(pwg_query($query));
+$row = pwg_db_fetch_assoc(pwg_query($query));
 
 $storage_category_id = null;
@@ -325,5 +325,5 @@
 $result = pwg_query($query);
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $name =
@@ -418,5 +418,5 @@
   array_push($associateds, $storage_category_id);
 }
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($associateds, $row['id']);
Index: /trunk/admin/rating.php
===================================================================
--- /trunk/admin/rating.php	(revision 4324)
+++ /trunk/admin/rating.php	(revision 4325)
@@ -95,5 +95,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $users[$row['id']]=stripslashes($row['username']);
@@ -105,5 +105,5 @@
 WHERE r.element_id=i.id'. $page['user_filter'] .
 ';';
-list($nb_images) = mysql_fetch_row(pwg_query($query));
+list($nb_images) = pwg_db_fetch_row(pwg_query($query));
 
 
@@ -182,5 +182,5 @@
 $images = array();
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($images, $row);
@@ -200,5 +200,5 @@
 ORDER BY date DESC;';
   $result = pwg_query($query);
-  $nb_rates = mysql_num_rows($result);
+  $nb_rates = pwg_db_num_rows($result);
 
   $tpl_image = 
@@ -215,5 +215,5 @@
    );
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
 
Index: /trunk/admin/site_manager.php
===================================================================
--- /trunk/admin/site_manager.php	(revision 4324)
+++ /trunk/admin/site_manager.php	(revision 4325)
@@ -99,5 +99,5 @@
   WHERE galleries_url = \''.$url.'\'
 ;';
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
   if ($row['count'] > 0)
   {
@@ -168,5 +168,5 @@
   WHERE id = '.$page['site'].'
 ;';
-  list($galleries_url) = mysql_fetch_row(pwg_query($query));
+  list($galleries_url) = pwg_db_fetch_row(pwg_query($query));
   switch($_GET['action'])
   {
@@ -237,5 +237,5 @@
 $result = pwg_query($query);
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $is_remote = url_is_remote($row['galleries_url']);
Index: /trunk/admin/site_update.php
===================================================================
--- /trunk/admin/site_update.php	(revision 4324)
+++ /trunk/admin/site_update.php	(revision 4325)
@@ -44,5 +44,5 @@
   FROM '.SITES_TABLE.'
   WHERE id = '.$site_id;
-list($site_url) = mysql_fetch_row(pwg_query($query));
+list($site_url) = pwg_db_fetch_row(pwg_query($query));
 if (!isset($site_url))
 {
@@ -51,5 +51,5 @@
 $site_is_remote = url_is_remote($site_url);
 
-list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 define('CURRENT_DATE', $dbnow);
 
@@ -186,5 +186,5 @@
   FROM '.CATEGORIES_TABLE;
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $next_rank[$row['id']] = 1;
@@ -197,5 +197,5 @@
   GROUP BY id_uppercat';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     // for the id_uppercat NULL, we write 'NULL' and not the empty string
@@ -211,5 +211,5 @@
 SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id
   FROM '.CATEGORIES_TABLE;
-  list($next_id) = mysql_fetch_row(pwg_query($query));
+  list($next_id) = pwg_db_fetch_row(pwg_query($query));
 
   // retrieve sub-directories fulldirs from the site reader
@@ -247,6 +247,6 @@
           ? 'false'
           : boolean_to_string($conf['newcat_default_uploadable']),
-        'status'      => $conf{'newcat_default_status'},
-        'visible'     => boolean_to_string($conf{'newcat_default_visible'}),
+        'status'      => $conf['newcat_default_status'],
+        'visible'     => boolean_to_string($conf['newcat_default_visible']),
         );
 
@@ -255,24 +255,24 @@
         $parent = $db_fulldirs[dirname($fulldir)];
 
-        $insert{'id_uppercat'} = $parent;
-        $insert{'uppercats'} =
-          $db_categories[$parent]['uppercats'].','.$insert{'id'};
-        $insert{'rank'} = $next_rank[$parent]++;
-        $insert{'global_rank'} =
-          $db_categories[$parent]['global_rank'].'.'.$insert{'rank'};
+        $insert['id_uppercat'] = $parent;
+        $insert['uppercats'] =
+          $db_categories[$parent]['uppercats'].','.$insert['id'];
+        $insert['rank'] = $next_rank[$parent]++;
+        $insert['global_rank'] =
+          $db_categories[$parent]['global_rank'].'.'.$insert['rank'];
         if ('private' == $db_categories[$parent]['status'])
         {
-          $insert{'status'} = 'private';
+          $insert['status'] = 'private';
         }
         if ('false' == $db_categories[$parent]['visible'])
         {
-          $insert{'visible'} = 'false';
+          $insert['visible'] = 'false';
         }
       }
       else
       {
-        $insert{'uppercats'} = $insert{'id'};
+        $insert['uppercats'] = $insert['id'];
         $insert{'rank'} = $next_rank['NULL']++;
-        $insert{'global_rank'} = $insert{'rank'};
+        $insert['global_rank'] = $insert['rank'];
       }
 
@@ -289,11 +289,11 @@
       $db_categories[$insert{'id'}] =
         array(
-          'id' => $insert{'id'},
-          'status' => $insert{'status'},
-          'visible' => $insert{'visible'},
-          'uppercats' => $insert{'uppercats'},
-          'global_rank' => $insert{'global_rank'}
+          'id' => $insert['id'],
+          'status' => $insert['status'],
+          'visible' => $insert['visible'],
+          'uppercats' => $insert['uppercats'],
+          'global_rank' => $insert['global_rank']
           );
-      $db_fulldirs[$fulldir] = $insert{'id'};
+      $db_fulldirs[$fulldir] = $insert['id'];
       $next_rank[$insert{'id'}] = 1;
     }
@@ -387,5 +387,5 @@
     AND validated = \'false\'';
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push(
@@ -403,5 +403,5 @@
 SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
   FROM '.IMAGES_TABLE;
-  list($next_element_id) = mysql_fetch_row(pwg_query($query));
+  list($next_element_id) = pwg_db_fetch_row(pwg_query($query));
 
   $start = get_moment();
@@ -471,5 +471,5 @@
         $insert_links,
         array(
-          'image_id'    => $insert{'id'},
+          'image_id'    => $insert['id'],
           'category_id' => $insert['storage_category_id'],
           )
@@ -479,5 +479,5 @@
         $infos,
         array(
-          'path' => $insert{'path'},
+          'path' => $insert['path'],
           'info' => l10n('update_research_added')
           )
@@ -556,5 +556,5 @@
     $waiting_to_delete = array();
 
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $data = array();
@@ -565,5 +565,5 @@
   WHERE storage_category_id = '.$row['storage_category_id'].'
     AND file = \''.$row['file'].'\'';
-      list($data['id']) = mysql_fetch_row(pwg_query($query));
+      list($data['id']) = pwg_db_fetch_row(pwg_query($query));
 
       foreach ($fields['update'] as $field)
Index: /trunk/admin/stats.php
===================================================================
--- /trunk/admin/stats.php	(revision 4324)
+++ /trunk/admin/stats.php	(revision 4325)
@@ -96,5 +96,5 @@
 
   $output = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($output, $row);
@@ -134,5 +134,5 @@
 $first_time_key = null;
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $time_keys = array(
@@ -205,5 +205,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $key = sprintf('%4u', $row['year']);
Index: /trunk/admin/tags.php
===================================================================
--- /trunk/admin/tags.php	(revision 4324)
+++ /trunk/admin/tags.php	(revision 4325)
@@ -50,5 +50,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $current_name_of[ $row['id'] ] = $row['name'];
@@ -221,5 +221,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $name_of[ $row['id'] ] = $row['name'];
Index: /trunk/admin/template/goto/intro.tpl
===================================================================
--- /trunk/admin/template/goto/intro.tpl	(revision 4324)
+++ /trunk/admin/template/goto/intro.tpl	(revision 4325)
@@ -31,5 +31,5 @@
       <li>PHP: {$PHP_VERSION} (<a href="{$U_PHPINFO}" onclick="window.open(this.href, ''); 
           return false;">{'Show info'|@translate}</a>)  [{$PHP_DATATIME}]</li>
-      <li>MySQL: {$MYSQL_VERSION} [{$DB_DATATIME}]</li>
+      <li>{$DB_ENGINE}: {$DB_VERSION} [{$DB_DATATIME}]</li>
     </ul>
   </dd>
Index: /trunk/admin/thumbnail.php
===================================================================
--- /trunk/admin/thumbnail.php	(revision 4324)
+++ /trunk/admin/thumbnail.php	(revision 4325)
@@ -179,5 +179,5 @@
 ;';
 $result = pwg_query($query);
-while ( $row=mysql_fetch_assoc($result) )
+while ( $row=pwg_db_fetch_assoc($result) )
 {
   $basedir = preg_replace('#/*$#', '', $row['galleries_url']);
Index: /trunk/admin/upload.php
===================================================================
--- /trunk/admin/upload.php	(revision 4324)
+++ /trunk/admin/upload.php	(revision 4325)
@@ -101,5 +101,5 @@
 ;';
     $result = pwg_query($query);
-    while($row = mysql_fetch_assoc($result))
+    while($row = pwg_db_fetch_assoc($result))
     {
       $dir = get_complete_dir($row['storage_category_id']);
@@ -154,5 +154,5 @@
 $query.= ';';
 $result = pwg_query( $query );
-while ( $row = mysql_fetch_assoc( $result ) )
+while ( $row = pwg_db_fetch_assoc( $result ) )
 {
   if ( !isset( $cat_names[$row['storage_category_id']] ) )
Index: /trunk/admin/user_list.php
===================================================================
--- /trunk/admin/user_list.php	(revision 4324)
+++ /trunk/admin/user_list.php	(revision 4325)
@@ -50,5 +50,5 @@
   {
     $username = str_replace('*', '%', $_GET['username']);
-    $filter['username'] = mysql_real_escape_string($username);
+    $filter['username'] = pwg_db_real_escape_string($username);
   }
 
@@ -116,5 +116,5 @@
 
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $user = $row;
@@ -140,5 +140,5 @@
 ;';
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push(
@@ -493,5 +493,5 @@
 $result = pwg_query($query);
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $groups[$row['id']] = $row['name'];
Index: /trunk/admin/user_perm.php
===================================================================
--- /trunk/admin/user_perm.php	(revision 4324)
+++ /trunk/admin/user_perm.php	(revision 4325)
@@ -79,5 +79,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($private_uppercats, $row['id']);
@@ -96,5 +96,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($authorized_ids, $row['cat_id']);
@@ -156,8 +156,8 @@
 $result = pwg_query($query);
 
-if (mysql_num_rows($result) > 0)
+if (pwg_db_num_rows($result) > 0)
 {
   $cats = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($cats, $row);
@@ -192,5 +192,5 @@
 $result = pwg_query($query_true);
 $authorized_ids = array();
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($authorized_ids, $row['id']);
Index: /trunk/comments.php
===================================================================
--- /trunk/comments.php	(revision 4324)
+++ /trunk/comments.php	(revision 4325)
@@ -284,5 +284,5 @@
     AND ', $page['where_clauses']).'
 ;';
-list($counter) = mysql_fetch_row(pwg_query($query));
+list($counter) = pwg_db_fetch_row(pwg_query($query));
 
 $url = PHPWG_ROOT_PATH
@@ -332,5 +332,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($comments, $row);
@@ -349,5 +349,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $elements[$row['id']] = $row;
Index: /trunk/feed.php
===================================================================
--- /trunk/feed.php	(revision 4324)
+++ /trunk/feed.php	(revision 4325)
@@ -76,5 +76,5 @@
   WHERE id = \''.$feed_id.'\'
 ;';
-  $feed_row = mysql_fetch_assoc(pwg_query($query));
+  $feed_row = pwg_db_fetch_assoc(pwg_query($query));
   if ( empty($feed_row) )
   {
@@ -98,5 +98,5 @@
 check_status(ACCESS_GUEST);
 
-list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
 include_once(PHPWG_ROOT_PATH.'include/feedcreator.class.php');
Index: /trunk/include/calendar_monthly.class.php
===================================================================
--- /trunk/include/calendar_monthly.class.php	(revision 4324)
+++ /trunk/include/calendar_monthly.class.php	(revision 4325)
@@ -232,5 +232,5 @@
   $result = pwg_query($query);
   $items=array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $y = substr($row['period'], 0, 4);
@@ -284,5 +284,5 @@
   $result = pwg_query($query);
   $items=array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $m = (int)substr($row['period'], 0, 2);
@@ -334,5 +334,5 @@
   $items=array();
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $d = (int)$row['period'];
@@ -352,5 +352,5 @@
     unset ( $page['chronology_date'][CDAY] );
 
-    $row = mysql_fetch_assoc(pwg_query($query));
+    $row = pwg_db_fetch_assoc(pwg_query($query));
     $items[$day]['tn_url'] = get_thumbnail_url($row);
     $items[$day]['file'] = $row['file'];
Index: /trunk/include/category_cats.inc.php
===================================================================
--- /trunk/include/category_cats.inc.php	(revision 4324)
+++ /trunk/include/category_cats.inc.php	(revision 4325)
@@ -76,5 +76,5 @@
 $image_ids = array();
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
@@ -109,7 +109,7 @@
 ;';
       $subresult = pwg_query($query);
-      if (mysql_num_rows($subresult) > 0)
+      if (pwg_db_num_rows($subresult) > 0)
       {
-        list($image_id) = mysql_fetch_row($subresult);
+        list($image_id) = pwg_db_fetch_row($subresult);
       }
     }
@@ -137,7 +137,7 @@
   ;';
       $subresult = pwg_query($query);
-      if (mysql_num_rows($subresult) > 0)
+      if (pwg_db_num_rows($subresult) > 0)
       {
-        list($image_id) = mysql_fetch_row($subresult);
+        list($image_id) = pwg_db_fetch_row($subresult);
       }
     }
@@ -179,5 +179,5 @@
 ;';
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $dates_of_category[ $row['category_id'] ] = array(
@@ -203,5 +203,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
Index: /trunk/include/category_default.inc.php
===================================================================
--- /trunk/include/category_default.inc.php	(revision 4324)
+++ /trunk/include/category_default.inc.php	(revision 4325)
@@ -46,5 +46,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $row['rank'] = $rank_of[ $row['id'] ];
Index: /trunk/include/common.inc.php
===================================================================
--- /trunk/include/common.inc.php	(revision 4324)
+++ /trunk/include/common.inc.php	(revision 4325)
@@ -93,4 +93,5 @@
 include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
 @include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
+include(PHPWG_ROOT_PATH .'include/dblayer/functions_mysql.inc.php');
 
 if(isset($conf['show_php_errors']) && !empty($conf['show_php_errors']))
@@ -105,20 +106,9 @@
 
 // Database connection
-@mysql_connect( $conf['db_host'], $conf['db_user'], $conf['db_password'] ) or my_error( 'mysql_connect', true );
-@mysql_select_db( $conf['db_base'] ) or my_error( 'mysql_select_db', true );
-
-defined('PWG_CHARSET') and defined('DB_CHARSET')
-  or fatal_error('PWG_CHARSET and/or DB_CHARSET is not defined');
-if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
-{
-  if (DB_CHARSET!='')
-  {
-    pwg_query('SET NAMES "'.DB_CHARSET.'"');
-  }
-}
-elseif ( strtolower(PWG_CHARSET)!='iso-8859-1' )
-{
-  fatal_error('PWG supports only iso-8859-1 charset on MySql version '.mysql_get_server_info());
-}
+$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], 
+			      $conf['db_password'], $conf['db_base']) 
+  or my_error('pwg_db_connect', true);
+
+pwg_db_check_charset();
 
 load_conf_from_db();
Index: /trunk/include/dblayer/functions_mysql.inc.php
===================================================================
--- /trunk/include/dblayer/functions_mysql.inc.php	(revision 4325)
+++ /trunk/include/dblayer/functions_mysql.inc.php	(revision 4325)
@@ -0,0 +1,463 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery                                  |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
+// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+/**
+ *
+ * simple functions
+ *
+ */
+
+function pwg_db_connect($host, $user, $password, $database)
+{  
+  $link = mysql_connect($host, $user, $password) or my_error('mysql_connect', true);
+  mysql_select_db($database, $link) or my_error('mysql_select_db', true);
+
+  return $link;
+}
+
+function pwg_db_check_charset() 
+{
+  defined('PWG_CHARSET') and defined('DB_CHARSET')
+    or fatal_error('PWG_CHARSET and/or DB_CHARSET is not defined');
+  if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
+  {
+    if (DB_CHARSET!='')
+    {
+      pwg_query('SET NAMES "'.DB_CHARSET.'"');
+    }
+  }
+  elseif ( strtolower(PWG_CHARSET)!='iso-8859-1' )
+  {
+    fatal_error('PWG supports only iso-8859-1 charset on MySql version '.mysql_get_server_info());
+  }
+}
+
+function pwg_get_db_version() 
+{
+  list($mysql_version) = pwg_db_fetch_row(pwg_query('SELECT VERSION();'));
+  
+  return $mysql_version;
+}
+
+function pwg_query($query)
+{
+  global $conf,$page,$debug,$t2;
+
+  $start = get_moment();
+  ($result = mysql_query($query)) or my_error($query, $conf['die_on_sql_error']);
+
+  $time = get_moment() - $start;
+
+  if (!isset($page['count_queries']))
+  {
+    $page['count_queries'] = 0;
+    $page['queries_time'] = 0;
+  }
+
+  $page['count_queries']++;
+  $page['queries_time']+= $time;
+
+  if ($conf['show_queries'])
+  {
+    $output = '';
+    $output.= '<pre>['.$page['count_queries'].'] ';
+    $output.= "\n".$query;
+    $output.= "\n".'(this query time : ';
+    $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>';
+    $output.= "\n".'(total SQL time  : ';
+    $output.= number_format($page['queries_time'], 3, '.', ' ').' s)';
+    $output.= "\n".'(total time      : ';
+    $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)';
+    if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) )
+    {
+      $output.= "\n".'(num rows        : ';
+      $output.= mysql_num_rows($result).' )';
+    }
+    elseif ( $result!=null
+      and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) )
+    {
+      $output.= "\n".'(affected rows   : ';
+      $output.= mysql_affected_rows().' )';
+    }
+    $output.= "</pre>\n";
+
+    $debug .= $output;
+  }
+
+  return $result;
+}
+
+function pwg_db_changes($result) 
+{
+  return mysql_affected_rows($result);
+}
+
+function pwg_db_num_rows($result) 
+{
+  return mysql_num_rows($result);
+}
+
+function pwg_db_fetch_assoc($result)
+{
+  return mysql_fetch_assoc($result);
+}
+
+function pwg_db_fetch_row($result)
+{
+  return mysql_fetch_row($result);
+}
+
+function pwg_db_fetch_object($result)
+{
+  return mysql_fetch_object($result);
+}
+
+function pwg_free_result($result) 
+{
+  return mysql_free_result($result);
+}
+
+function pwg_db_real_escape_string($s)
+{
+  return mysql_real_escape_string($s);
+}
+
+function pwg_db_insert_id()
+{
+  return mysql_insert_id();
+}
+
+/**
+ *
+ * complex functions
+ *
+ */
+
+/**
+ * creates an array based on a query, this function is a very common pattern
+ * used here
+ *
+ * @param string $query
+ * @param string $fieldname
+ * @return array
+ */
+function array_from_query($query, $fieldname)
+{
+  $array = array();
+
+  $result = pwg_query($query);
+  while ($row = mysql_fetch_assoc($result))
+  {
+    array_push($array, $row[$fieldname]);
+  }
+
+  return $array;
+}
+
+define('MASS_UPDATES_SKIP_EMPTY', 1);
+/**
+ * updates multiple lines in a table
+ *
+ * @param string table_name
+ * @param array dbfields
+ * @param array datas
+ * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
+ * @return void
+ */
+function mass_updates($tablename, $dbfields, $datas, $flags=0)
+{
+  if (count($datas) == 0)
+    return;
+  // depending on the MySQL version, we use the multi table update or N update queries
+  if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0)
+  { // MySQL is prior to version 4.0.4, multi table update feature is not available
+    foreach ($datas as $data)
+    {
+      $query = '
+UPDATE '.$tablename.'
+  SET ';
+      $is_first = true;
+      foreach ($dbfields['update'] as $key)
+      {
+        $separator = $is_first ? '' : ",\n    ";
+
+        if (isset($data[$key]) and $data[$key] != '')
+        {
+          $query.= $separator.$key.' = \''.$data[$key].'\'';
+        }
+        else
+        {
+          if ($flags & MASS_UPDATES_SKIP_EMPTY )
+            continue; // next field
+          $query.= "$separator$key = NULL";
+        }
+        $is_first = false;
+      }
+      if (!$is_first)
+      {// only if one field at least updated
+        $query.= '
+  WHERE ';
+        $is_first = true;
+        foreach ($dbfields['primary'] as $key)
+        {
+          if (!$is_first)
+          {
+            $query.= ' AND ';
+          }
+          if ( isset($data[$key]) )
+          {
+            $query.= $key.' = \''.$data[$key].'\'';
+          }
+          else
+          {
+            $query.= $key.' IS NULL';
+          }
+          $is_first = false;
+        }
+        pwg_query($query);
+      }
+    } // foreach update
+  } // if mysql_ver or count<X
+  else
+  {
+    // creation of the temporary table
+    $query = '
+SHOW FULL COLUMNS FROM '.$tablename;
+    $result = pwg_query($query);
+    $columns = array();
+    $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
+    while ($row = pwg_db_fetch_assoc($result))
+    {
+      if (in_array($row['Field'], $all_fields))
+      {
+        $column = $row['Field'];
+        $column.= ' '.$row['Type'];
+
+        $nullable = true;
+        if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO')
+        {
+          $column.= ' NOT NULL';
+          $nullable = false;
+        }
+        if (isset($row['Default']))
+        {
+          $column.= " default '".$row['Default']."'";
+        }
+        elseif ($nullable)
+        {
+          $column.= " default NULL";
+        }
+        if (isset($row['Collation']) and $row['Collation'] != 'NULL')
+        {
+          $column.= " collate '".$row['Collation']."'";
+        }
+        array_push($columns, $column);
+      }
+    }
+
+    $temporary_tablename = $tablename.'_'.micro_seconds();
+
+    $query = '
+CREATE TABLE '.$temporary_tablename.'
+(
+  '.implode(",\n  ", $columns).',
+  UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).')
+)';
+
+    pwg_query($query);
+    mass_inserts($temporary_tablename, $all_fields, $datas);
+    if ( $flags & MASS_UPDATES_SKIP_EMPTY )
+      $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";');
+    else
+      $func_set = create_function('$s', 'return "t1.$s = t2.$s";');
+
+    // update of images table by joining with temporary table
+    $query = '
+UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2
+  SET '.
+      implode(
+        "\n    , ",
+        array_map($func_set,$dbfields['update'])
+        ).'
+  WHERE '.
+      implode(
+        "\n    AND ",
+        array_map(
+          create_function('$s', 'return "t1.$s = t2.$s";'),
+          $dbfields['primary']
+          )
+        );
+    pwg_query($query);
+    $query = '
+DROP TABLE '.$temporary_tablename;
+    pwg_query($query);
+  }
+}
+
+
+/**
+ * inserts multiple lines in a table
+ *
+ * @param string table_name
+ * @param array dbfields
+ * @param array inserts
+ * @return void
+ */
+function mass_inserts($table_name, $dbfields, $datas)
+{
+  if (count($datas) != 0)
+  {
+    $first = true;
+
+    $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
+    list(, $packet_size) = pwg_db_fetch_row(pwg_query($query));
+    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
+    $query = '';
+
+    foreach ($datas as $insert)
+    {
+      if (strlen($query) >= $packet_size)
+      {
+        pwg_query($query);
+        $first = true;
+      }
+
+      if ($first)
+      {
+        $query = '
+INSERT INTO '.$table_name.'
+  ('.implode(',', $dbfields).')
+  VALUES';
+        $first = false;
+      }
+      else
+      {
+        $query .= '
+  , ';
+      }
+
+      $query .= '(';
+      foreach ($dbfields as $field_id => $dbfield)
+      {
+        if ($field_id > 0)
+        {
+          $query .= ',';
+        }
+
+        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
+        {
+          $query .= 'NULL';
+        }
+        else
+        {
+          $query .= "'".$insert[$dbfield]."'";
+        }
+      }
+      $query .= ')';
+    }
+    Log::getInstance()->debug($query);
+    pwg_query($query);
+  }
+}
+
+/**
+ * Do maintenance on all PWG tables
+ *
+ * @return none
+ */
+function do_maintenance_all_tables()
+{
+  global $prefixeTable, $page;
+
+  $all_tables = array();
+
+  // List all tables
+  $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\'';
+  $result = pwg_query($query);
+  while ($row = pwg_db_fetch_assoc($result))
+  {
+    array_push($all_tables, $row[0]);
+  }
+
+  // Repair all tables
+  $query = 'REPAIR TABLE '.implode(', ', $all_tables);
+  $mysql_rc = pwg_query($query);
+
+  // Re-Order all tables
+  foreach ($all_tables as $table_name)
+  {
+    $all_primary_key = array();
+
+    $query = 'DESC '.$table_name.';';
+    $result = pwg_query($query);
+    while ($row = pwg_db_fetch_assoc($result))
+    {
+      if ($row['Key'] == 'PRI')
+      {
+        array_push($all_primary_key, $row['Field']);
+      }
+    }
+
+    if (count($all_primary_key) != 0)
+    {
+      $query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';';
+      $mysql_rc = $mysql_rc && pwg_query($query);
+    }
+  }
+
+  // Optimize all tables
+  $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables);
+  $mysql_rc = $mysql_rc && pwg_query($query);
+  if ($mysql_rc)
+  {
+    array_push(
+          $page['infos'],
+          l10n('Optimizations completed')
+          );
+  }
+  else
+  {
+    array_push(
+          $page['errors'],
+          l10n('Optimizations errors')
+          );
+  }
+}
+
+// my_error returns (or send to standard output) the message concerning the
+// error occured for the last mysql query.
+function my_error($header, $die)
+{
+  $error = "[mysql error ".mysql_errno().'] '.mysql_error()."\n";
+  $error .= $header;
+
+  if ($die)
+  {
+    fatal_error($error);
+  }
+  echo("<pre>");
+  trigger_error($error, E_USER_WARNING);
+  echo("</pre>");
+}
+
+?>
Index: /trunk/include/functions.inc.php
===================================================================
--- /trunk/include/functions.inc.php	(revision 4324)
+++ /trunk/include/functions.inc.php	(revision 4325)
@@ -45,5 +45,5 @@
   // columns are 'Field', 'Type'
   $result = pwg_query('desc '.$table);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     // we are only interested in the the field given in parameter for the
@@ -60,5 +60,5 @@
     }
   }
-  mysql_free_result($result);
+  pwg_db_free_result($result);
   return $options;
 }
@@ -597,52 +597,4 @@
 }
 
-function pwg_query($query)
-{
-  global $conf,$page,$debug,$t2;
-
-  $start = get_moment();
-  ($result = mysql_query($query)) or my_error($query, $conf['die_on_sql_error']);
-
-  $time = get_moment() - $start;
-
-  if (!isset($page['count_queries']))
-  {
-    $page['count_queries'] = 0;
-    $page['queries_time'] = 0;
-  }
-
-  $page['count_queries']++;
-  $page['queries_time']+= $time;
-
-  if ($conf['show_queries'])
-  {
-    $output = '';
-    $output.= '<pre>['.$page['count_queries'].'] ';
-    $output.= "\n".$query;
-    $output.= "\n".'(this query time : ';
-    $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>';
-    $output.= "\n".'(total SQL time  : ';
-    $output.= number_format($page['queries_time'], 3, '.', ' ').' s)';
-    $output.= "\n".'(total time      : ';
-    $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)';
-    if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) )
-    {
-      $output.= "\n".'(num rows        : ';
-      $output.= mysql_num_rows($result).' )';
-    }
-    elseif ( $result!=null
-      and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) )
-    {
-      $output.= "\n".'(affected rows   : ';
-      $output.= mysql_affected_rows().' )';
-    }
-    $output.= "</pre>\n";
-
-    $debug .= $output;
-  }
-
-  return $result;
-}
-
 function pwg_debug( $string )
 {
@@ -904,42 +856,4 @@
 
   return $thumbnail_title;
-}
-
-// my_error returns (or send to standard output) the message concerning the
-// error occured for the last mysql query.
-function my_error($header, $die)
-{
-  $error = "[mysql error ".mysql_errno().'] '.mysql_error()."\n";
-  $error .= $header;
-
-  if ($die)
-  {
-    fatal_error($error);
-  }
-  echo("<pre>");
-  trigger_error($error, E_USER_WARNING);
-  echo("</pre>");
-}
-
-
-/**
- * creates an array based on a query, this function is a very common pattern
- * used here
- *
- * @param string $query
- * @param string $fieldname
- * @return array
- */
-function array_from_query($query, $fieldname)
-{
-  $array = array();
-
-  $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
-  {
-    array_push($array, $row[$fieldname]);
-  }
-
-  return $array;
 }
 
@@ -1122,5 +1036,5 @@
   WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].'
 ;';
-  list($email) = mysql_fetch_row(pwg_query($query));
+  list($email) = pwg_db_fetch_row(pwg_query($query));
 
   return $email;
@@ -1143,10 +1057,10 @@
   $result = pwg_query($query);
 
-  if ((mysql_num_rows($result) == 0) and !empty($condition))
+  if ((pwg_db_num_rows($result) == 0) and !empty($condition))
   {
     fatal_error('No configuration data');
   }
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $conf[ $row['param'] ] = isset($row['value']) ? $row['value'] : '';
@@ -1193,5 +1107,5 @@
 
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $array[ $row[$keyname] ] = $row[$valuename];
@@ -1214,5 +1128,5 @@
   $array = array();
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $array[ $row[$keyname] ] = $row;
@@ -1585,5 +1499,5 @@
     // Use MySql date in order to standardize all recent "actions/queries"
     list($cache['get_icon']['sql_recent_date']) =
-      mysql_fetch_row(pwg_query('select SUBDATE(
+      pwg_db_fetch_row(pwg_query('select SUBDATE(
       CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'));
   }
Index: /trunk/include/functions_category.inc.php
===================================================================
--- /trunk/include/functions_category.inc.php	(revision 4324)
+++ /trunk/include/functions_category.inc.php	(revision 4325)
@@ -102,5 +102,5 @@
   $cats = array();
   $selected_category = isset($page['category']) ? $page['category'] : null;
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $child_date_last = @$row['max_date_last']> @$row['date_last'];
@@ -166,5 +166,5 @@
   WHERE id = '.$id.'
 ;';
-  $cat = mysql_fetch_assoc(pwg_query($query));
+  $cat = pwg_db_fetch_assoc(pwg_query($query));
   if (empty($cat))
     return null;
@@ -241,5 +241,5 @@
     $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
     $query.= ';';
-    $row = mysql_fetch_assoc( pwg_query( $query ) );
+    $row = pwg_db_fetch_assoc( pwg_query( $query ) );
     $uppercats = $row['uppercats'];
   }
@@ -252,5 +252,5 @@
   $query.= ';';
   $result = pwg_query( $query );
-  while( $row = mysql_fetch_assoc( $result ) )
+  while( $row = pwg_db_fetch_assoc( $result ) )
   {
     $database_dirs[$row['id']] = $row['dir'];
@@ -276,5 +276,5 @@
     AND c.id = '.$category_id.'
 ;';
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
   return $row['galleries_url'];
 }
@@ -345,5 +345,5 @@
   if (!empty($result))
   {
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push($categories, $row);
@@ -385,5 +385,5 @@
 
   $subcats = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($subcats, $row['id']);
@@ -534,5 +534,5 @@
 LIMIT 1';
 
-      $show_link = mysql_num_rows(pwg_query($query)) <> 0;
+      $show_link = pwg_db_num_rows(pwg_query($query)) <> 0;
     }
   }
Index: /trunk/include/functions_comment.inc.php
===================================================================
--- /trunk/include/functions_comment.inc.php	(revision 4324)
+++ /trunk/include/functions_comment.inc.php	(revision 4325)
@@ -101,5 +101,5 @@
   FROM '.USERS_TABLE.'
   WHERE '.$conf['user_fields']['username']." = '".addslashes($comm['author'])."'";
-      $row = mysql_fetch_assoc( pwg_query( $query ) );
+      $row = pwg_db_fetch_assoc( pwg_query( $query ) );
       if ( $row['user_exists'] == 1 )
       {
@@ -139,5 +139,5 @@
   WHERE date > FROM_UNIXTIME('.$reference_date.')
     AND author_id = '.$comm['author_id'];
-    if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
+    if ( pwg_db_num_rows( pwg_query( $query ) ) > 0 )
     {
       array_push( $infos, l10n('comment_anti-flood') );
@@ -169,5 +169,5 @@
     pwg_query($query);
 
-    $comm['id'] = mysql_insert_id();
+    $comm['id'] = pwg_db_insert_id();
 
     if (($comment_action=='validate' and $conf['email_admin_on_comment']) or
@@ -267,5 +267,5 @@
   WHERE date > FROM_UNIXTIME('.$reference_date.')
     AND author_id = '.$comm['author_id'];
-    if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
+    if ( pwg_db_num_rows( pwg_query( $query ) ) > 0 )
     {
       //?? array_push( $infos, l10n('comment_anti-flood') );
Index: /trunk/include/functions_mail.inc.php
===================================================================
--- /trunk/include/functions_mail.inc.php	(revision 4324)
+++ /trunk/include/functions_mail.inc.php	(revision 4325)
@@ -352,5 +352,5 @@
   if (!empty($datas))
   {
-    while ($admin = mysql_fetch_assoc($datas))
+    while ($admin = pwg_db_fetch_assoc($datas))
     {
       if (!empty($admin['mail_address']))
@@ -448,8 +448,8 @@
   $result = pwg_query($query);
 
-  if (mysql_num_rows($result) > 0)
+  if (pwg_db_num_rows($result) > 0)
   {
     $list = array();
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $row['template_theme'] = $row['template'];
@@ -477,8 +477,8 @@
       $result = pwg_query($query);
 
-      if (mysql_num_rows($result) > 0)
+      if (pwg_db_num_rows($result) > 0)
       {
         $Bcc = array();
-        while ($row = mysql_fetch_assoc($result))
+        while ($row = pwg_db_fetch_assoc($result))
         {
           if (!empty($row['mail_address']))
Index: /trunk/include/functions_notification.inc.php
===================================================================
--- /trunk/include/functions_notification.inc.php	(revision 4324)
+++ /trunk/include/functions_notification.inc.php	(revision 4325)
@@ -146,5 +146,5 @@
     $query = 'SELECT count(distinct '.$field_id.') as CountId
 '.$query;
-    list($count) = mysql_fetch_row(pwg_query($query));
+    list($count) = pwg_db_fetch_row(pwg_query($query));
     return $count;
 
@@ -179,5 +179,5 @@
     $infos = array();
 
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push($infos, $row);
@@ -449,5 +449,5 @@
   $result = pwg_query($query);
   $dates = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($dates, $row);
@@ -469,5 +469,5 @@
       $dates[$i]['elements'] = array();
       $result = pwg_query($query);
-      while ($row = mysql_fetch_assoc($result))
+      while ($row = pwg_db_fetch_assoc($result))
       {
         array_push($dates[$i]['elements'], $row);
@@ -489,5 +489,5 @@
       $dates[$i]['categories'] = array();
       $result = pwg_query($query);
-      while ($row = mysql_fetch_assoc($result))
+      while ($row = pwg_db_fetch_assoc($result))
       {
         array_push($dates[$i]['categories'], $row);
Index: /trunk/include/functions_plugins.inc.php
===================================================================
--- /trunk/include/functions_plugins.inc.php	(revision 4324)
+++ /trunk/include/functions_plugins.inc.php	(revision 4325)
@@ -239,5 +239,5 @@
   $result = pwg_query($query);
   $plugins = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($plugins, $row);
Index: /trunk/include/functions_rate.inc.php
===================================================================
--- /trunk/include/functions_rate.inc.php	(revision 4324)
+++ /trunk/include/functions_rate.inc.php	(revision 4325)
@@ -125,5 +125,5 @@
   WHERE element_id = '.$image_id.'
 ;';
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
   $query = '
 UPDATE '.IMAGES_TABLE.'
Index: /trunk/include/functions_search.inc.php
===================================================================
--- /trunk/include/functions_search.inc.php	(revision 4324)
+++ /trunk/include/functions_search.inc.php	(revision 4325)
@@ -42,5 +42,5 @@
   WHERE id = '.$search_id.'
 ;';
-  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
+  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
 
   return unserialize($serialized_rules);
@@ -459,5 +459,5 @@
   $by_weights=array();
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   { // weight is important when sorting images by relevance
     if ($row['weight'])
@@ -490,5 +490,5 @@
   GROUP BY image_id';
       $result = pwg_query($query);
-      while ($row = mysql_fetch_assoc($result))
+      while ($row = pwg_db_fetch_assoc($result))
       { // weight is important when sorting images by relevance
         $image_id=(int)$row['image_id'];
@@ -511,5 +511,5 @@
     );
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   { // weight is important when sorting images by relevance
     if ($row['nb_images']==0)
Index: /trunk/include/functions_session.inc.php
===================================================================
--- /trunk/include/functions_session.inc.php	(revision 4324)
+++ /trunk/include/functions_session.inc.php	(revision 4325)
@@ -122,5 +122,5 @@
   if ($result)
   {
-    $row = mysql_fetch_assoc($result);
+    $row = pwg_db_fetch_assoc($result);
     return $row['data'];
   }
Index: /trunk/include/functions_tag.inc.php
===================================================================
--- /trunk/include/functions_tag.inc.php	(revision 4324)
+++ /trunk/include/functions_tag.inc.php	(revision 4325)
@@ -64,5 +64,5 @@
   $result = pwg_query($query);
   $tags = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $counter = @$tag_counters[ $row['id'] ];
@@ -89,5 +89,5 @@
   $result = pwg_query($query);
   $tags = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($tags, $row);
@@ -250,5 +250,5 @@
   $result = pwg_query($query);
   $tags = array();
-  while($row = mysql_fetch_assoc($result))
+  while($row = pwg_db_fetch_assoc($result))
   {
     array_push($tags, $row);
@@ -312,5 +312,5 @@
   $result = pwg_query($query);
   $tags = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($tags, $row);
Index: /trunk/include/functions_user.inc.php
===================================================================
--- /trunk/include/functions_user.inc.php	(revision 4324)
+++ /trunk/include/functions_user.inc.php	(revision 4325)
@@ -59,5 +59,5 @@
 '.(is_numeric($user_id) ? 'and '.$conf['user_fields']['id'].' != \''.$user_id.'\'' : '').'
 ;';
-    list($count) = mysql_fetch_row(pwg_query($query));
+    list($count) = pwg_db_fetch_row(pwg_query($query));
     if ($count != 0)
     {
@@ -111,10 +111,10 @@
   FROM '.USERS_TABLE.'
 ;';
-    list($next_id) = mysql_fetch_row(pwg_query($query));
+    list($next_id) = pwg_db_fetch_row(pwg_query($query));
 
     $insert =
       array(
         $conf['user_fields']['id'] => $next_id,
-        $conf['user_fields']['username'] => mysql_real_escape_string($login),
+        $conf['user_fields']['username'] => pwg_db_real_escape_string($login),
         $conf['user_fields']['password'] => $conf['pass_convert']($password),
         $conf['user_fields']['email'] => $mail_address
@@ -135,5 +135,5 @@
 
       $inserts = array();
-      while ($row = mysql_fetch_assoc($result))
+      while ($row = pwg_db_fetch_assoc($result))
       {
         array_push
@@ -250,5 +250,5 @@
   WHERE '.$conf['user_fields']['id'].' = \''.$user_id.'\'';
 
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
 
   while (true)
@@ -260,5 +260,5 @@
   WHERE ui.user_id = \''.$user_id.'\'';
     $result = pwg_query($query);
-    if (mysql_num_rows($result) > 0)
+    if (pwg_db_num_rows($result) > 0)
     {
       break;
@@ -270,5 +270,5 @@
   }
 
-  $row = array_merge($row, mysql_fetch_assoc($result));
+  $row = array_merge($row, pwg_db_fetch_assoc($result));
 
   foreach ($row as $key => $value)
@@ -325,5 +325,5 @@
   WHERE category_id NOT IN ('.$userdata['forbidden_categories'].')
     AND image_id '.$userdata['image_access_type'].' ('.$userdata['image_access_list'].')';
-      list($userdata['nb_total_images']) = mysql_fetch_row(pwg_query($query));
+      list($userdata['nb_total_images']) = pwg_db_fetch_row(pwg_query($query));
 
 
@@ -429,5 +429,5 @@
   $result = pwg_query($query);
   $authorizeds = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($authorizeds, $row['image_id']);
@@ -441,5 +441,5 @@
   $result = pwg_query($query);
   $favorites = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($favorites, $row['image_id']);
@@ -482,5 +482,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($private_array, $row['id']);
@@ -524,5 +524,5 @@
 ;';
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push($forbidden_array, $row['id']);
@@ -648,5 +648,5 @@
 
   $cats = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $row['user_id'] = $userdata['id'];
@@ -693,5 +693,5 @@
   global $conf;
 
-  $username = mysql_real_escape_string($username);
+  $username = pwg_db_real_escape_string($username);
 
   $query = '
@@ -702,5 +702,5 @@
   $result = pwg_query($query);
 
-  if (mysql_num_rows($result) == 0)
+  if (pwg_db_num_rows($result) == 0)
   {
     return false;
@@ -708,5 +708,5 @@
   else
   {
-    list($user_id) = mysql_fetch_row($result);
+    list($user_id) = pwg_db_fetch_row($result);
     return $user_id;
   }
@@ -728,5 +728,5 @@
   WHERE id = \''.$key.'\'
 ;';
-    list($count) = mysql_fetch_row(pwg_query($query));
+    list($count) = pwg_db_fetch_row(pwg_query($query));
     if (0 == $count)
     {
@@ -751,5 +751,5 @@
 
     $result = pwg_query($query);
-    $cache['default_user'] = mysql_fetch_assoc($result);
+    $cache['default_user'] = pwg_db_fetch_assoc($result);
 
     if ($cache['default_user'] !== false)
@@ -866,5 +866,5 @@
   {
     $inserts = array();
-    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
     $default_user = get_default_user_info(false);
@@ -931,7 +931,7 @@
 WHERE '.$conf['user_fields']['id'].' = '.$user_id;
   $result = pwg_query($query);
-  if (mysql_num_rows($result) > 0)
-  {
-    $row = mysql_fetch_assoc($result);
+  if (pwg_db_num_rows($result) > 0)
+  {
+    $row = pwg_db_fetch_assoc($result);
     $username = stripslashes($row['username']);
     $data = $time.stripslashes($row['username']).$row['password'];
@@ -1042,5 +1042,5 @@
   WHERE '.$conf['user_fields']['username'].' = \''.mysql_real_escape_string($username).'\'
 ;';
-  $row = mysql_fetch_assoc(pwg_query($query));
+  $row = pwg_db_fetch_assoc(pwg_query($query));
   if ($row['password'] == $conf['pass_convert']($password))
   {
Index: /trunk/include/picture_comment.inc.php
===================================================================
--- /trunk/include/picture_comment.inc.php	(revision 4324)
+++ /trunk/include/picture_comment.inc.php	(revision 4325)
@@ -94,5 +94,5 @@
   FROM '.COMMENTS_TABLE.'
   WHERE image_id='.$page['image_id']." AND validated = 'true'";
-  $row = mysql_fetch_assoc( pwg_query( $query ) );
+  $row = pwg_db_fetch_assoc( pwg_query( $query ) );
 
   // navigation bar creation
@@ -141,5 +141,5 @@
     $result = pwg_query( $query );
 
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       if (!empty($row['author']))
Index: /trunk/include/picture_rate.inc.php
===================================================================
--- /trunk/include/picture_rate.inc.php	(revision 4324)
+++ /trunk/include/picture_rate.inc.php	(revision 4325)
@@ -38,5 +38,5 @@
   WHERE element_id = '.$picture['current']['id'].'
 ;';
-    $row = mysql_fetch_assoc(pwg_query($query));
+    $row = pwg_db_fetch_assoc(pwg_query($query));
   }
   else
@@ -68,7 +68,7 @@
 
       $result = pwg_query($query);
-      if (mysql_num_rows($result) > 0)
+      if (pwg_db_num_rows($result) > 0)
       {
-        $row = mysql_fetch_assoc($result);
+        $row = pwg_db_fetch_assoc($result);
         $user_rate = $row['rate'];
       }
Index: /trunk/include/ws_functions.inc.php
===================================================================
--- /trunk/include/ws_functions.inc.php	(revision 4324)
+++ /trunk/include/ws_functions.inc.php	(revision 4325)
@@ -257,5 +257,5 @@
   $result = pwg_query($query);
   $cats = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $row['id'] = (int)$row['id'];
@@ -296,5 +296,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $image = array();
@@ -406,5 +406,5 @@
 
   $cats = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $row['url'] = make_index_url(
@@ -472,5 +472,5 @@
   $cats = array();
 
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     $id = $row['id'];
@@ -518,5 +518,5 @@
       ' AND'
     );
-  if ( !mysql_num_rows( pwg_query( $query ) ) )
+  if ( !pwg_db_num_rows( pwg_query( $query ) ) )
   {
     return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
@@ -578,5 +578,5 @@
 LIMIT 1';
 
-  $image_row = mysql_fetch_assoc(pwg_query($query));
+  $image_row = pwg_db_fetch_assoc(pwg_query($query));
   if ($image_row==null)
   {
@@ -599,5 +599,5 @@
   $is_commentable = false;
   $related_categories = array();
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     if ($row['commentable']=='true')
@@ -656,5 +656,5 @@
   WHERE element_id = '.$image_row['id'].'
 ;';
-  $rating = mysql_fetch_assoc(pwg_query($query));
+  $rating = pwg_db_fetch_assoc(pwg_query($query));
   $rating['count'] = (int)$rating['count'];
 
@@ -687,5 +687,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $row['id']=(int)$row['id'];
@@ -758,5 +758,5 @@
     ).'
     LIMIT 1';
-  if ( mysql_num_rows( pwg_query($query) )==0 )
+  if ( pwg_db_num_rows( pwg_query($query) )==0 )
   {
     return new PwgError(404, "Invalid image_id or access denied" );
@@ -814,5 +814,5 @@
     $image_ids = array_flip($image_ids);
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $image = array();
@@ -871,5 +871,5 @@
   WHERE id IN ('.implode(',',$params['image_id']).')';
   $result = pwg_query($query);
-  $affected_rows = mysql_affected_rows();
+  $affected_rows = pwg_db_affected_rows();
   if ($affected_rows)
   {
@@ -1008,5 +1008,5 @@
   WHERE md5sum = \''.$params['original_sum'].'\'
 ;';
-  list($counter) = mysql_fetch_row(pwg_query($query));
+  list($counter) = pwg_db_fetch_row(pwg_query($query));
   if ($counter != 0) {
     return new PwgError(500, 'file already exists');
@@ -1014,5 +1014,5 @@
 
   // current date
-  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
   list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
 
@@ -1200,5 +1200,5 @@
     );
 
-  $image_id = mysql_insert_id();
+  $image_id = pwg_db_insert_id();
 
   // let's add links between the image and the categories
@@ -1359,5 +1359,5 @@
   GROUP BY image_id';
       $result = pwg_query($query);
-      while ( $row=mysql_fetch_assoc($result) )
+      while ( $row=pwg_db_fetch_assoc($result) )
       {
         $row['image_id'] = (int)$row['image_id'];
@@ -1402,5 +1402,5 @@
 
     $result = pwg_query($query);
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       $image = array();
@@ -1573,5 +1573,5 @@
 ;';
 
-  $image_row = mysql_fetch_assoc(pwg_query($query));
+  $image_row = pwg_db_fetch_assoc(pwg_query($query));
   if ($image_row == null)
   {
Index: /trunk/install.php
===================================================================
--- /trunk/install.php	(revision 4324)
+++ /trunk/install.php	(revision 4325)
@@ -365,5 +365,5 @@
     // make PWG avoid upgrading, we must tell it upgrades have already been
     // made.
-    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     define('CURRENT_DATE', $dbnow);
     $datas = array();
Index: /trunk/install/db/1-database.php
===================================================================
--- /trunk/install/db/1-database.php	(revision 4324)
+++ /trunk/install/db/1-database.php	(revision 4325)
@@ -33,5 +33,5 @@
 // +-----------------------------------------------------------------------+
 
-list($now) = mysql_fetch_row(pwg_query('SELECT NOW()'));
+list($now) = pwg_db_fetch_row(pwg_query('SELECT NOW()'));
 echo
 $now
Index: /trunk/install/db/12-database.php
===================================================================
--- /trunk/install/db/12-database.php	(revision 4324)
+++ /trunk/install/db/12-database.php	(revision 4325)
@@ -67,5 +67,5 @@
 $datas = array();
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push(
Index: /trunk/install/db/18-database.php
===================================================================
--- /trunk/install/db/18-database.php	(revision 4324)
+++ /trunk/install/db/18-database.php	(revision 4325)
@@ -48,5 +48,5 @@
 $datas = array();
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push(
Index: /trunk/install/db/19-database.php
===================================================================
--- /trunk/install/db/19-database.php	(revision 4324)
+++ /trunk/install/db/19-database.php	(revision 4325)
@@ -69,5 +69,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
Index: /trunk/install/db/20-database.php
===================================================================
--- /trunk/install/db/20-database.php	(revision 4324)
+++ /trunk/install/db/20-database.php	(revision 4325)
@@ -48,5 +48,5 @@
 
 $datas = array();
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push(
Index: /trunk/install/db/65-database.php
===================================================================
--- /trunk/install/db/65-database.php	(revision 4324)
+++ /trunk/install/db/65-database.php	(revision 4325)
@@ -109,5 +109,5 @@
   GROUP BY language';
   $result = pwg_query($query);
-  while ( $row=mysql_fetch_assoc($result) )
+  while ( $row=pwg_db_fetch_assoc($result) )
   {
     $language = $row["language"];
@@ -142,5 +142,5 @@
   WHERE user_id='.$conf['webmaster_id'];
   $result = pwg_query($query);
-  if (mysql_num_rows($result)==0)
+  if (pwg_db_num_rows($result)==0)
   {
     $query='
@@ -151,5 +151,5 @@
   }
 
-  if ( $row=mysql_fetch_assoc($result) )
+  if ( $row=pwg_db_fetch_assoc($result) )
   {
     $admin_charset = $all_langs[$row['language']]['charset'];
@@ -166,5 +166,5 @@
   $query = 'SHOW TABLES LIKE "'.$prefixeTable.'%"';
   $result = pwg_query($query);
-  while ( $row=mysql_fetch_assoc($result) )
+  while ( $row=pwg_db_fetch_assoc($result) )
   {
     array_push($all_tables, $row[0]);
@@ -177,5 +177,5 @@
     $result = pwg_query($query);
     $field_definitions=array();
-    while ( $row=mysql_fetch_assoc($result) )
+    while ( $row=pwg_db_fetch_assoc($result) )
     {
       if ( !isset($row['Collation']) or $row['Collation']=='NULL' )
Index: /trunk/install/db/8-database.php
===================================================================
--- /trunk/install/db/8-database.php	(revision 4324)
+++ /trunk/install/db/8-database.php	(revision 4325)
@@ -61,5 +61,5 @@
 $query = 'SELECT param FROM '.PREFIX_TABLE.'config';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   unset( $params[ $row['param'] ] );
Index: /trunk/install/db/9-database.php
===================================================================
--- /trunk/install/db/9-database.php	(revision 4324)
+++ /trunk/install/db/9-database.php	(revision 4325)
@@ -48,5 +48,5 @@
 $datas = array();
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push(
Index: /trunk/install/upgrade_1.3.0.php
===================================================================
--- /trunk/install/upgrade_1.3.0.php	(revision 4324)
+++ /trunk/install/upgrade_1.3.0.php	(revision 4325)
@@ -85,5 +85,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '')
Index: /trunk/install/upgrade_1.3.1.php
===================================================================
--- /trunk/install/upgrade_1.3.1.php	(revision 4324)
+++ /trunk/install/upgrade_1.3.1.php	(revision 4325)
@@ -43,5 +43,5 @@
   FROM '.PREFIX_TABLE.'config
 ;';
-$save = mysql_fetch_assoc(pwg_query($query));
+$save = pwg_db_fetch_assoc(pwg_query($query));
 
 $queries = array(
@@ -345,5 +345,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     if ($row['Key_name'] != 'PRIMARY')
@@ -537,5 +537,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($cat_ids, $row['unique_storage_category_id']);
@@ -562,5 +562,5 @@
 ;';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($cat_ids, $row['id']);
Index: /trunk/install/upgrade_1.4.0.php
===================================================================
--- /trunk/install/upgrade_1.4.0.php	(revision 4324)
+++ /trunk/install/upgrade_1.4.0.php	(revision 4325)
@@ -43,5 +43,5 @@
   WHERE param = \'prefix_thumbnail\'
 ;';
-list($prefix_thumbnail) = mysql_fetch_row(pwg_query($query));
+list($prefix_thumbnail) = pwg_db_fetch_row(pwg_query($query));
 
 // delete obsolete configuration
@@ -192,8 +192,8 @@
 
 $datas = array();
-list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   $row['user_id'] = $row['id'];
Index: /trunk/install/upgrade_1.5.0.php
===================================================================
--- /trunk/install/upgrade_1.5.0.php	(revision 4324)
+++ /trunk/install/upgrade_1.5.0.php	(revision 4325)
@@ -80,5 +80,5 @@
 ;';
   $result = pwg_query($query);
-  while ($row = mysql_fetch_assoc($result))
+  while ($row = pwg_db_fetch_assoc($result))
   {
     foreach(preg_split('/[,]+/', $row['keywords']) as $keyword)
@@ -352,5 +352,5 @@
 $query = 'SELECT param FROM '.PREFIX_TABLE.'config';
 $result = pwg_query($query);
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   unset( $params[ $row['param'] ] );
@@ -453,5 +453,5 @@
 $result = pwg_query($query);
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   if (!in_array($row['Field'], $to_keep))
Index: /trunk/install/upgrade_1.7.0.php
===================================================================
--- /trunk/install/upgrade_1.7.0.php	(revision 4324)
+++ /trunk/install/upgrade_1.7.0.php	(revision 4325)
@@ -36,5 +36,5 @@
 define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
 
-list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
+list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 define('CURRENT_DATE', $dbnow);
 
Index: /trunk/password.php
===================================================================
--- /trunk/password.php	(revision 4324)
+++ /trunk/password.php	(revision 4325)
@@ -57,5 +57,5 @@
   else if (isset($_POST['mail_address']) and !empty($_POST['mail_address']))
   {
-    $mail_address = mysql_real_escape_string($_POST['mail_address']);
+    $mail_address = pwg_db_real_escape_string($_POST['mail_address']);
     
     $query = '
@@ -75,10 +75,10 @@
     $result = pwg_query($query);
 
-    if (mysql_num_rows($result) > 0)
+    if (pwg_db_num_rows($result) > 0)
     {
       $error_on_mail = false;
       $datas = array();
       
-      while ($row = mysql_fetch_assoc($result))
+      while ($row = pwg_db_fetch_assoc($result))
       {
         $new_password = generate_key(6);
Index: /trunk/picture.php
===================================================================
--- /trunk/picture.php	(revision 4324)
+++ /trunk/picture.php	(revision 4325)
@@ -58,5 +58,5 @@
       '.%" ESCAPE "/" LIMIT 1';
   }
-  if ( ! ( $row = mysql_fetch_assoc(pwg_query($query)) ) )
+  if ( ! ( $row = pwg_db_fetch_assoc(pwg_query($query)) ) )
   {// element does not exist
     page_not_found( 'The requested image does not exist',
@@ -94,5 +94,5 @@
           ).'
   LIMIT 1';
-      if ( mysql_num_rows( pwg_query($query) ) == 0 )
+      if ( pwg_db_num_rows( pwg_query($query) ) == 0 )
       {
         access_denied();
@@ -388,5 +388,5 @@
 $result = pwg_query($query);
 $related_categories = array();
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   array_push($related_categories, $row);
@@ -416,5 +416,5 @@
 $result = pwg_query($query);
 
-while ($row = mysql_fetch_assoc($result))
+while ($row = pwg_db_fetch_assoc($result))
 {
   if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
@@ -768,5 +768,5 @@
 ;';
   $result = pwg_query($query);
-  $row = mysql_fetch_assoc($result);
+  $row = pwg_db_fetch_assoc($result);
 
   if ($row['nb_fav'] == 0)
Index: /trunk/profile.php
===================================================================
--- /trunk/profile.php	(revision 4324)
+++ /trunk/profile.php	(revision 4325)
@@ -56,5 +56,5 @@
 ;';
     $result = pwg_query($query);
-    $default_user = mysql_fetch_assoc($result);
+    $default_user = pwg_db_fetch_assoc($result);
     $userdata = array_merge($userdata, $default_user);
   }
@@ -163,5 +163,5 @@
     WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
   ;';
-      list($current_password) = mysql_fetch_row(pwg_query($query));
+      list($current_password) = pwg_db_fetch_row(pwg_query($query));
   
       if ($conf['pass_convert']($_POST['password']) != $current_password)
Index: /trunk/qsearch.php
===================================================================
--- /trunk/qsearch.php	(revision 4324)
+++ /trunk/qsearch.php	(revision 4325)
@@ -61,5 +61,5 @@
 ;';
   pwg_query($query);
-  $search_id = mysql_insert_id();
+  $search_id = pwg_db_insert_id();
 }
 
Index: /trunk/search.php
===================================================================
--- /trunk/search.php	(revision 4324)
+++ /trunk/search.php	(revision 4325)
@@ -137,5 +137,5 @@
     pwg_query($query);
 
-    $search_id = mysql_insert_id();
+    $search_id = pwg_db_insert_id();
   }
   else
Index: /trunk/search_rules.php
===================================================================
--- /trunk/search_rules.php	(revision 4324)
+++ /trunk/search_rules.php	(revision 4325)
@@ -136,5 +136,5 @@
   if (!empty($result))
   {
-    while ($row = mysql_fetch_assoc($result))
+    while ($row = pwg_db_fetch_assoc($result))
     {
       array_push($categories, $row);
Index: /trunk/upgrade.php
===================================================================
--- /trunk/upgrade.php	(revision 4324)
+++ /trunk/upgrade.php	(revision 4325)
@@ -44,4 +44,5 @@
 include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
 @include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
+include(PHPWG_ROOT_PATH .'include/dblayer/functions_mysql.inc.php');
 
 prepare_conf_upgrade();
@@ -51,11 +52,9 @@
 
 // Database connection
-mysql_connect( $conf['db_host'], $conf['db_user'], $conf['db_password'] ) or die ( "Could not connect to database server" );
-mysql_select_db( $conf['db_base'] ) or die ( "Could not connect to database" );
-if ( version_compare(mysql_get_server_info(), '4.1.0', '>=')
-    and defined('DB_CHARSET') and DB_CHARSET!='' )
-{
-  pwg_query('SET NAMES "'.DB_CHARSET.'"');
-}
+$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], 
+			      $conf['db_password'], $conf['db_base']) 
+  or my_error('pwg_db_connect', true);
+
+pwg_db_check_charset();
 
 // +-----------------------------------------------------------------------+
@@ -77,5 +76,5 @@
   $result = pwg_query($query);
 
-  while ($row = mysql_fetch_row($result))
+  while ($row = pwg_db_fetch_row($result))
   {
     if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
@@ -106,5 +105,5 @@
     $columns_of[$table] = array();
 
-    while ($row = mysql_fetch_row($result))
+    while ($row = pwg_db_fetch_row($result))
     {
       array_push($columns_of[$table], $row[0]);
Index: /trunk/upgrade_feed.php
===================================================================
--- /trunk/upgrade_feed.php	(revision 4324)
+++ /trunk/upgrade_feed.php	(revision 4325)
@@ -36,4 +36,5 @@
 include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
 @include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
+include(PHPWG_ROOT_PATH .'include/dblayer/functions_mysql.inc.php');
 
 // +-----------------------------------------------------------------------+
@@ -55,12 +56,9 @@
 // +-----------------------------------------------------------------------+
 
-mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_password']) or die("Could not connect to database server");
-mysql_select_db($conf['db_base']) or die("Could not connect to database");
-if ( version_compare(mysql_get_server_info(), '4.1.0', '>=')
-    and defined('DB_CHARSET') and DB_CHARSET!='' )
-{
-  pwg_query('SET NAMES "'.DB_CHARSET.'"');
-}
+$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], 
+			      $conf['db_password'], $conf['db_base']) 
+  or my_error('pwg_db_connect', true);
 
+pwg_db_check_charset();
 
 // +-----------------------------------------------------------------------+
Index: /trunk/upload.php
===================================================================
--- /trunk/upload.php	(revision 4324)
+++ /trunk/upload.php	(revision 4325)
@@ -249,5 +249,5 @@
     $query.= ';';
     pwg_query( $query );
-    $page['waiting_id'] = mysql_insert_id();
+    $page['waiting_id'] = pwg_db_insert_id();
 
     if ($conf['email_admin_on_picture_uploaded'])
@@ -289,5 +289,5 @@
   $query.= ';';
   $result= pwg_query( $query );
-  $row = mysql_fetch_assoc( $result );
+  $row = pwg_db_fetch_assoc( $result );
   $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
   $extension = get_extension( $_FILES['picture']['name'] );
