将网站从MySQL迁移到MySQLi

时间:2015-09-13 12:14:41

标签: php mysqli

如您所知,MySQL已被弃用,我开始迁移到MySQLi,因为没有人知道什么时候会从php中删除mysql函数。

我差不多已经完成了,但是我从互联网上复制了一个脚本中的问题。

问题是 function openDB()会创建与数据库的连接:

$datba1 = @mysqli_connect($dserver, $duser, $dpass) or die('Error');

但是在相同类$datba1函数_getRecords 中丢失了。我添加了global $datba1;,但它没有帮助,查询产生错误:mysqli_query() expects at least 2 parameters, 1 given.....

我有2个文件:

sc_select_ajax_handle.php

include_once 'config.inc.php';
include("sc_classes.php");

sc_ajax_select_boxes::openDB(C_HOST, C_USER, C_PASS);

    if( isset($_REQUEST['class']) )
      $class = $_REQUEST['class'];
    else
      $class =''; 

    if( $class == 'updater' ) {
      $link_field_value = $_REQUEST['linkval'];
      $table = $_REQUEST['table'];
      $keyfld = $_REQUEST['keyfld'];
      $keyval = $_REQUEST['keyval'];
      $flds = $_REQUEST['flds'];
      $xml_encoding = $_REQUEST['xml_encoding'];

      $sc_ajax_updater = new sc_ajax_updater();

      $rows = $sc_ajax_updater->ajax_get_records($table, $keyfld, $keyval, $flds);
      if( count($rows)!=0 || $link_field_value=='0' )
          $sc_ajax_updater->ajax_output_xml($rows, $xml_encoding);
      else
          $sc_ajax_updater->ajax_output_xml_for_none($xml_encoding);
    }    
    else {
      $link_field_value = $_REQUEST['linkval'];
      $table = $_REQUEST['table'];
      $key = $_REQUEST['key'];
      $text = $_REQUEST['text'];
      $order = $_REQUEST['order'];
      $extra_where = stripslashes($_REQUEST['extra_where']);
      $select_prompt_text = $_REQUEST['select_prompt_text'];
      $linkfld = $_REQUEST['linkfld'];
      $xml_encoding = $_REQUEST['xml_encoding'];
      $sc_ajax_select_boxes = new sc_ajax_select_boxes();

      $rows = $sc_ajax_select_boxes->ajax_get_records($table, $key, $text, $order, 
                                              $linkfld, $link_field_value,
                                              $extra_where);
      if( count($rows)!=0 || $link_field_value=='0' )
          $sc_ajax_select_boxes->ajax_output_xml($rows, $select_prompt_text, $xml_encoding);
      else
          $sc_ajax_select_boxes->ajax_output_xml_for_none($xml_encoding);

    }

sc_classes.php (文件sc_classes非常大,所以我只在这里发布了一部分)

class sc_ajax_select_boxes {
        var $php_ajax_handler = '/sc_select_ajax_handler.php';

        var $select_prompt_key = 0;
        var $select_prompt_text = 'Please Select';
        var $select_add_prompt_row = TRUE;
        var $select_wait_text = '...';
        var $select_empty_text = '(-)';
        var $select_not_found_text = '(Not found)';

        var $select_boxes = array(); 
        var $select_box_links = array(); 

        var $xml_encoding = 'ISO-8859-1';

        var $group_pre_group = '';
        var $group_post_group = '';
        var $group_pre_input = '';
        var $group_post_input = '<br />';

        var $group_wrap_every = 0;  // 0 for no wrap
        var $group_pre_wrap = '';
        var $group_post_wrap = '<br />';

        /* 
            DB helper functions 
         */
        function _getRecords($query) {
        global $datba1;
        if(!isset($datba1)) {
        mail('my@email.com', 'no conenction', "no connection to the database");
         }
            $rows = array();
            $result = mysqli_query($datba1,$query);
            if($result != false ) {
                if( mysqli_num_rows($result)!=0 ) {

                    while ($line = mysqli_fetch_array($result, MYSQL_NUM)) {
                        $rows[] = $line;
                    }
                }
            } 


            return( $rows );
        }   // end function _getRecords ---------------------------------------

        function _getRecordsAssoc($query) {
        global $datba1;


            $rows = array();
            $result = mysqli_query($datba1,$query);
            if($result != false ) {
                if( mysqli_num_rows($result)!=0 ) {
                    while ($line = mysqli_fetch_assoc($result)) {
                        $rows[] = $line;
                    }
                }
            }
            return( $rows );
        }   // end function _getRecords ---------------------------------------

        function openDB($dserver, $duser, $dpass) {

        $resp=0;

          $datba1 = @mysqli_connect($dserver, $duser, $dpass) or die('Error');

          if (!$datba1) {
          $resp=-1;
          } else {
          $resp=0;
          }

            if( $resp!=0 ) {
                if( isset($datba1) )
                    unset($datba1);
            }

            return( $resp );
        }   // end function openDB ----------------------------------

        function get_list_from_db($table, $key_fld, $text_fld, $order_fld='', 
                                  $link_field="", $link_field_value="",
                                  $extra_where="") {
            $b_where_word_added=FALSE;                        
            $cmd = "SELECT $key_fld, $text_fld FROM $table ";
            if( (!empty($link_field)) && (!empty($link_field_value)) ) {
                $b_where_word_added = TRUE;
                $cmd .= " WHERE $link_field= ";
                if( is_string($link_field_value) )
                    $cmd .="'$link_field_value' ";
                else
                    $cmd .="$link_field_value ";
            }
            if( !empty($extra_where ) ) {
                if( $b_where_word_added ) 
                    $cmd .= " AND ";
                else
                    $cmd .= " WHERE ";
                $cmd .= " $extra_where ";
            }
            if( $order_fld!='' )
                $cmd .= " ORDER BY $order_fld";

            $rows = $this->_getRecords($cmd);
            $list = array();
            if( count($rows)!=0 ) {
                foreach($rows as $row) {
                    $list[$row[0]] = $row[1];
                }
            }

            return($list);
        }   // end function get_list_from_db 

0 个答案:

没有答案