I'm testing my script in PHP 5.5, works perfectly until I switch from MySQL to MySQLi.

Error:

Stack trace:

#0 C:\xampp\htdocs\url_rewrite.php(52): tep_db_fetch_array(NULL)
#1 C:\xampp\htdocs\url_rewrite.php(106): transform_uri(Array)
#2 [internal function]: wrap_href(Array)
#3 C:\xampp\htdocs\url_rewrite.php(12): preg_replace_callback('/(<[Aa][ ???]{1...', 'wrap_href', '<!DOCTYPE html>...')
#4 [internal function]: callback('<!DOCTYPE html>...', 9)
#5 {main}

Line 52 of url_rewrite:

$cat_Q = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $value . "'  and c.categories_id = cd.categories_id");

I'm not sure what I should be doing differently here. As mentioned, the script works without issue on MySQL.
Any help would be greatly appreciated!

Recommended Answers

All 9 Replies

I have tried relentlessly to use the proper code/quote syntax, but the forum keeps telling me it's incorrect. Sorry. :(

I went ahead and fixed your code. We don't use BBCode around here like most other forums do. Instead, we use the Markdown syntax. There's a link to documentation in the top right of every editor. Or it's here: http://www.daniweb.com/community/syntax

Are you sure that you have MySQLi installed??

Yes MySQLi is installed, I just upgraded a different script to MySQLi using the same folder space.

How are you switching from MySQL to MySQLi in OSCommerce?

Switching database.php to database_mysqli.php via application_top.php:

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2007 osCommerce

  Released under the GNU General Public License
*/

  function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
    global $$link;

    $$link = mysqli_connect($server, $username, $password, $database);

    //if (!$$link) die("Can not access database");

    return $$link;
  }

  function tep_db_close($link = 'db_link') {
    global $$link;

    return mysqli_close($$link);
  }

  function tep_db_error($query, $errno, $error) { 
    die('<font color="#000000"><strong>' . $errno . ' - ' . $error . '<br /><br />' . $query . '<br /><br /><small><font color="#ff0000">[TEP STOP]</font></small><br /><br /></strong></font>');
  }

  function tep_db_query($query, $link = 'db_link') {
    global $$link;

    if (!$$link) return;

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
      $debugs = debug_backtrace();
      $debug = isset($debugs[1]) ?  $debugs[1] : null;
      if ($debug) {
          if (tep_not_null($debug['function'])) {
              error_log('CALLEE ' . $debug['file'] . ' (line: '.$debug['line'].' function: '.$debug['function'].') ' . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
          } else {
              error_log('CALLEE ' . $debug['file'] . ' (line: '.$debug['line'].') ' . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
          }
      }
      error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    $result = mysqli_query($$link, $query) or tep_db_error($query, mysqli_errno($$link), mysqli_error($$link));

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
       $result_error = mysqli_error();
       error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    return $result;
  }

  function tep_db_multi_query($query, $link = 'db_link') {
    global $$link;

    if (!$$link) return;
    if (!is_array($query)) return tep_db_query($query, $link);

    $query_text = is_array($query)? implode(";", $query) : $query;

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
      error_log('QUERY ' . $query_text . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    $results = array();
    if (mysqli_multi_query($$link, $query_text)) {
        do {
            /* store first result set */
            if ($result = mysqli_store_result($$link)) {
                $results[] = $result;
            }
        } while (mysqli_more_results($$link) && mysqli_next_result($$link));
    } else {
        tep_db_error($query_text, mysqli_errno(), mysqli_error());
    }

    if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
       $result_error = mysqli_error();
       error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
    }

    return $results;
  }

  function tep_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
    reset($data);
    if ($action == 'insert') {
      $query = 'insert into ' . $table . ' (';
      while (list($columns, ) = each($data)) {
        $query .= $columns . ', ';
      }
      $query = substr($query, 0, -2) . ') values (';
      reset($data);
      while (list(, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= 'now(), ';
            break;
          case 'null':
            $query .= 'null, ';
            break;
          default:
            $query .= '\'' . tep_db_input($value) . '\', ';
            break;
        }
      }
      $query = substr($query, 0, -2) . ')';
    } elseif ($action == 'update') {
      $query = 'update ' . $table . ' set ';
      while (list($columns, $value) = each($data)) {
        switch ((string)$value) {
          case 'now()':
            $query .= $columns . ' = now(), ';
            break;
          case 'null':
            $query .= $columns .= ' = null, ';
            break;
          default:
            $query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
            break;
        }
      }
      $query = substr($query, 0, -2) . ' where ' . $parameters;
    }

    return tep_db_query($query, $link);
  }

  function tep_db_fetch_array($db_query, $type=MYSQLI_ASSOC) {
   if ($db_query) {
    return mysqli_fetch_array($db_query, $type);
   }else{
      throw new Exception(mysqli_error($$link).". Full query: [$db_query]");
   }

  }

  function tep_db_num_rows($db_query) {
    return mysqli_num_rows($db_query);
  }

  function tep_db_data_seek($db_query, $row_number) {
    return mysqli_data_seek($db_query, $row_number);
  }

  function tep_db_insert_id($link = 'db_link') {
    global $$link;

    return mysqli_insert_id($$link);
  }

  function tep_db_free_result($db_query) {
    return @mysqli_free_result($db_query);
  }

  function tep_db_fetch_fields($db_query) {
    return mysqli_fetch_field($db_query);
  }

  function tep_db_output($string) {
    return htmlspecialchars($string);
  }

  function tep_db_mysqli($string) {
    return mysqli_real_escape_string($$link,$string);
  }

  function tep_db_input($string, $link = 'db_link') {
    global $$link;
    if (!$$link) return tep_db_prepare_input($string);

    if (function_exists('mysqli_real_escape_string')) {
      return mysqli_real_escape_string($$link, $string);
    } elseif (function_exists('mysqli_escape_string')) {
      return mysqli_escape_string($$link, $string);
    }

  function tep_db_mysqli($string) {
  // global $$link;
    return mysqli_real_escape_string($$link,$string);
  }

    return addslashes($string);
  }

  function tep_db_prepare_input($string) {
    if (is_string($string)) {
      return trim(tep_sanitize_string(stripslashes($string)));
    } elseif (is_array($string)) {
      reset($string);
      while (list($key, $value) = each($string)) {
        $string[$key] = tep_db_prepare_input($value);
      }
      return $string;
    } else {
      return $string;
    }
  }
?>

Sorry, it's bedtime. I won't be able to look at this again until the morning.

Aww :(

Any help at any time of the day is more than appreciated. Goodnight! :)

Any tips on what could be causing this, anyone?

After some additional digging, ob_start appears to be the culprint (used for URL rewrite). Perhaps something in the code is incompatible with MySQLi? Any way to pinpoint the source of the incompatibility?

<?php
function callback($pagecontent) {
  $pagecontent = preg_replace_callback("/(<[Aa][ \r\n\t]{1}[^>]*href[^=]*=[ '\"\n\r\t]*)([^ \"'>\r\n\t#]+)([^>]*>)/",'wrap_href',$pagecontent);
  return $pagecontent;
}

function transform_uri($param) {
//Define the result variable
$result = '';
  $uriparts = parse_url($param[2]);
  $newquery= '';
  $scheme = isset( $uriparts['scheme'] ) ? $uriparts['scheme'] : 'http';

  // no reformat on SSL addresses
  //if ( $scheme == 'https' ) return $param[0];
  $scheme .= '://';
$host = isset( $uriparts['host'] ) ? $uriparts['host'] : '';
  if ($host != $_SERVER['SERVER_NAME'] && $host != $_SERVER['SERVER_ADDR']) return $param[1].$param[2].$param[3];


  $path = $uriparts['path'];
$fileParts = explode('.', basename($path));
$file = isset($fileParts[0]) ? $fileParts[0] : '';
$extension = isset($fileParts[1]) ? $fileParts[1] : '';
  //list($file,$extension) = explode('.', basename($path));
  if($extension != 'php') return $param[1].$param[2].$param[3];
  $extension = ".html";
  $path = rtrim(dirname($path),'/');
  $query = isset( $uriparts['query'] ) ? $uriparts['query'] : '';
  $anchor = isset( $uriparts['anchor'] ) ? $uriparts['anchor'] : '';
$a = array();
//parse the string and put into the array $a
parse_str($query, $a);
if( !empty($a) ){
  foreach($a as $key => $val ){
     switch( $key ) {
        case 'cPath':
            if(preg_match('/^[_0-9]*$/',$val)){
              if($cat_arr = explode('_', $val)){
                $count = false;
                foreach($cat_arr as $value){
                  $cat_Q = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $value . "'  and c.categories_id = cd.categories_id") or die('mysqli error');
                  $cat_name = tep_db_fetch_array($cat_Q);
                  if(!$count){
                    $result .= $cat_name['categories_name'];
                    $count = true;
                  }
                  else{
                    $result .= '_' . $cat_name['categories_name'];
                  } 
                }
                $cat = '/category/'. str_replace(' ' , '-' , $result);
              }
              else{         
                $cat = '/category/'.$val;
              }
            }
            else{
              $cat = '/category/'.$val;
            }
            break;
        case 'language':
            $lan = $val.'/'.$path;
            break;
        case 'products_id':
            $name_Q = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . $val . "'");
            $pro = ($t = tep_db_fetch_array($name_Q)) ? '/product/' . str_replace(" ", "_" , $t['products_name']) : '/product/'.$val;
            break;
        case 'manufacturers_id':
            $manufacturers_Q = tep_db_query("select manufacturers_name, manufacturers_type from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . $val . "'");
            $man = ($t = tep_db_fetch_array($manufacturers_Q)) ? '/'.$t['manufacturers_type'].'/'.str_replace(" ", "_" , $t['manufacturers_name']) : $man = '/manufacturers/'.$val;
            break;
        case 'catid':
            if(strstr($_SERVER["HTTP_USER_AGENT"],'Mozilla'))  $newquery .= $key.'='.$val.'&';
            break;
        default:
            if($newquery || $key) $newquery .= $key.'='.$val.'&';     
      }
    }
  }
  if ($newquery) $newquery = '?'.rtrim($newquery,'&');
  $path = '';
  if(isset($man)) $path .= $man;
  if(isset($cat)) $path .= $cat;
  if(isset($pro)) $path .= $pro;

  ((isset($man) || isset($cat) || isset($pro))) ? $host .= '' :$host .= '/';
  if($file == 'index' || $file == 'product_info'){
    if((isset($man) || isset($cat) || isset($pro))) $file= '';
  }
  if(preg_match('/^reviews$/',$file)) $file = '/' . $file;
  return $param[1].$scheme.$host.$file.$path.$extension.$newquery.$anchor.$param[3];


}
function wrap_href($param) {
  return transform_uri($param);
}

ob_start("callback");
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.