diff --git a/webform_mysql_views.module b/webform_mysql_views.module
index fa2bb89..2b4e77c 100644
--- a/webform_mysql_views.module
+++ b/webform_mysql_views.module
@@ -1,4 +1,4 @@
-<?php // $Id$
+<?php
 
 /**
  * @file
@@ -7,6 +7,7 @@
  * applications.
  *
  * @author Andy Chase <andychase@gmail.com>
+ * @author Joe Corall <joe.corall@gmail.com>
  */
 
 /**
@@ -59,6 +60,7 @@ function webform_mysql_views_form_alter(&$form, &$form_state, $form_id) {
  * Submit handler for the webform component edit/delete forms.
  */
 function webform_mysql_views_component_submit($form, $form_state) {
+$f = fopen('/var/www/cph/t.txt','w');fwrite("FORM STATE === " . print_r($form_state));fclose($f);
   //If this node has a MySQL view, update it.
   switch ($form_state['values']['form_id']) {
     case 'webform_components_form':
@@ -118,6 +120,7 @@ function webform_mysql_views_rebuild($nid, $add_new = FALSE) {
       $name = $views[$nid];
     }
     $query = webform_mysql_views_build_query($nid, $name);
+
     db_query($query);
   }
 
@@ -136,7 +139,7 @@ function webform_mysql_views_rebuild($nid, $add_new = FALSE) {
 function webform_mysql_views_drop($nid) {
   $views = variable_get('webform_mysql_views_views', array());
   if (!empty($views[$nid])) {
-    db_query("DROP VIEW {%s}", $views[$nid]);
+    db_query("DROP VIEW {". $views[$nid] . "}");
     unset($views[$nid]);
     variable_set('webform_mysql_views_views', $views);
   }
@@ -170,32 +173,27 @@ function webform_mysql_views_help($path) {
  */
 function webform_mysql_views_admin_form() {
 
-
   $meets_reqs = _webform_mysql_views_check_requirements();
-  if ($meets_reqs !== TRUE) {
+  if (!$meets_reqs) {
     $output = '<strong>'. t("This module will not work on this site.") .'</strong>';
     $output .= theme('item_list', $meets_reqs, t('Details'));
-
     return array("error" => array('#value' => $output));
   }
 
-
   // Get list of webform-enabled content types
-  $types = webform_variable_get('webform_node_types', array());
+  $types = webform_variable_get('webform_node_types');
 
   //Get list of nids that already have views
   $views = variable_get('webform_mysql_views_views', array());
   $view_nids = array_keys($views);
 
   //Get list of all webform nodes
-  $query = db_rewrite_sql("SELECT nid, title FROM {node} n WHERE type IN ('%s') AND status = 1 ORDER BY nid");
-  $result = db_query($query, implode("','", $types));
-
-
+  $query = "SELECT nid, title FROM {node} n WHERE type IN('" . implode("','",$types) . "') AND status = 1 ORDER BY nid";
+  $result = db_query($query);
   $options = array();
   $titles = array();
   $default_value = array();
-  while ($node = db_fetch_object($result)) {
+  foreach ($result as $node) {
     $titles[$node->nid] = $node->title;
     if (in_array($node->nid, $view_nids)) {
       $default_value[$node->nid] = $node->nid;
@@ -211,7 +209,6 @@ function webform_mysql_views_admin_form() {
   }
 
   $form = array();
-
   //Pass the titles along
   $form['titles'] = array(
     '#type' => 'value',
@@ -236,6 +233,7 @@ function webform_mysql_views_admin_form() {
     '#type' => 'submit',
     '#value' => t('Submit'),
   );
+
   return $form;
 
 }
@@ -246,8 +244,8 @@ function webform_mysql_views_admin_form() {
  * @see webform_mysql_views_admin_form()
  */
 function webform_mysql_views_admin_form_submit($form, &$form_state) {
-
-  global $db_prefix;
+  global $databases;
+  $db_prefix = $databases['default']['default']['prefix'];
 
   //Load existing view info
   $webform_views = $previous_webform_views = variable_get('webform_mysql_views_views', array());
@@ -264,9 +262,9 @@ function webform_mysql_views_admin_form_submit($form, &$form_state) {
   //If there are any nids in the saved array not in the newly submitted one, drop those views.
   $drop_nids = array_diff($webform_view_nids, $submitted_nids);
   foreach ($drop_nids as $nid) {
-    $query = "DROP VIEW {%s}";
+    $query = "DROP VIEW {" . substr($webform_views[$nid], strlen($db_prefix)) . "}" ;
     // (Drop the prefix from the stored view name and let the DB API handle it)
-    db_query($query, substr($webform_views[$nid], strlen($db_prefix)));
+    db_query($query);
     unset($webform_views[$nid]);
   }
   if (count($drop_nids)) {
@@ -343,28 +341,33 @@ function webform_mysql_views_build_query($nid, $view_name) {
   // the MySQL server group_concat_max_length setting is usually limited to
   // 1024 characters, and it's easier to work around that here than tinkering
   // with the my.cnf file.
+  $query = "SELECT c.cid, c.form_key, c.type FROM {webform_component} c WHERE c.nid = "
+           . $nid 
+           . " AND c.type != 'fieldset' ORDER BY c.weight ASC, c.cid ASC";
 
-  $result = db_query('SELECT c.cid, c.form_key, c.type FROM {webform_component} c WHERE c.nid = %d AND c.type != "fieldset" ORDER BY c.weight ASC, c.cid ASC', $nid);
+  $result = db_query($query);
   $components = '';
 
-  while ($row = db_fetch_array($result)) {
-    switch ($row['type']) {
+  foreach ($result as $row) {
+    switch ($row->type) {
       //Collapse grid values in a string of key=val pairs
       case 'grid':
-        $component = sprintf("(SELECT GROUP_CONCAT(no,'=',data) FROM {webform_submitted_data} AS child WHERE child.sid = parent.sid AND cid = %d) AS `%s`, ", $row['cid'], $row['form_key']);
+        $component = sprintf("(SELECT GROUP_CONCAT(no,'=',data) FROM {webform_submitted_data} AS child WHERE child.sid = parent.sid AND cid = %d) AS `%s`, ", $row->cid, $row->form_key);
         break;
       //Otherwise collapse multi-value fields into simple comma-separated lists
       default:
-        $component = sprintf("(SELECT GROUP_CONCAT(data) FROM {webform_submitted_data} AS child WHERE child.sid = parent.sid AND cid = %d) AS `%s`, ", $row['cid'], $row['form_key']);
+        $component = sprintf("(SELECT GROUP_CONCAT(data) FROM {webform_submitted_data} AS child WHERE child.sid = parent.sid AND cid = %d) AS `%s`, ", $row->cid, $row->form_key);
     }
     $components .= $component;
   }
 
-  $query = "CREATE OR REPLACE VIEW {%s} AS SELECT parent.sid, s.uid,"
+  $query = "CREATE OR REPLACE VIEW {".$view_name."} AS SELECT parent.sid, s.uid,"
            . $components
-           .' FROM_UNIXTIME(s.submitted) AS submitted, s.remote_addr FROM {webform_submitted_data} AS parent JOIN {webform_submissions} s ON s.sid = parent.sid WHERE parent.nid = %d GROUP BY parent.sid ORDER BY parent.sid DESC;';
+           ." FROM_UNIXTIME(s.submitted) AS submitted, s.remote_addr FROM {webform_submitted_data} AS parent JOIN {webform_submissions} s ON s.sid = parent.sid WHERE parent.nid = "
+           . $nid
+           ." GROUP BY parent.sid ORDER BY parent.sid DESC";
 
-  return sprintf($query, $view_name, $nid);
+  return $query;
 }
 
 /**
@@ -389,10 +392,16 @@ function webform_mysql_views_get_view_name($title, $nid) {
   // Check whether the default view name is already being used
   // (For example duplicate node titles). Append $nid if necessary to ensure
   // uniqueness.  Table names not escaped as they are not a part of the Drupal DB.
+  $query = "SELECT COUNT(table_name) AS view_exists FROM information_schema.views where table_schema = '" 
+           . $db_name 
+           . "' AND table_name = '"
+           . $db_prefix
+           . $title 
+           ."'";
 
-  $view_exists = db_result(db_query("SELECT COUNT(table_name) AS view_exists FROM information_schema.views where table_schema = '%s' AND table_name = '%s%s'", $db_name, $db_prefix, $title));
+  $view_exists = db_query($query);
 
-  if ($view_exists) {
+  if (isset($view_exists)) {
     return $title .'_'. $nid;
   }
 
@@ -404,8 +413,6 @@ function webform_mysql_views_get_view_name($title, $nid) {
  */
 function _webform_mysql_views_check_requirements() {
 
-  global $db_url;
-
   $meets_reqs = variable_set('webform_mysql_views_meets_reqs', FALSE);
 
   if ($meets_reqs) {
@@ -413,17 +420,14 @@ function _webform_mysql_views_check_requirements() {
   }
   else {
     $errors = array();
+
     //Make sure site is using MySQL backend
-    if (substr($db_url, 0, 5) != 'mysql') {
+    global $databases;
+    $db_url = $databases['default']['default']['driver'];
+    if (!strcmp($db_url,'mysql')) {
       $errors[] = t("This module is only compatible with the MySQL backend.");
     }
 
-    //Make sure server major version is 5
-    $version = db_result(db_query("SELECT version() AS version"));
-    if ((int)substr($version, 0, 1) < 5) {
-      $errors[] = t("This module requires MySQL server version 5.0 or later.  Your server is running version @version.", array('@version' => $version));
-    }
-
     if (count($errors) == 0) {
       variable_set('webform_mysql_views_meets_reqs', TRUE);
       return TRUE;
