drtpbx3606 2014-01-29 11:03
浏览 30
已采纳

将mysql配置转换为mysqli [关闭]

I want to convert this little piece of code that I did with mysql into mysqli, anyone who can help it will be appreciated alot. I tried a couple of times to do it but I failed but I need to do it with mysqli because mysql in future will be depreciated. . .thank you in advance.

<?php

CLASS DB_Class {

  //database selection classes

  function DB_Class($dbname, $username, $password) {
    $this->db = mysql_connect ('host', 'guest', 'dbacess')
    or die ("Unable to connect to Database Server");
    mysql_select_db ($dbname, $this->db) or die ("Could not select database");
  }

  function query($sql) {
    $result = mysql_query ($sql, $this->db) or die ("Invalid query: " . mysql_error());
    return $result;
  }

  function fetch($sql) {
     $data = array();
     $result = $this->query($sql);

     while($row = MYSQL_FETCH_ASSOC($result)) {
          $data[] = $row;
     }
          return $data;
   }

   function getone($sql) {
   $result = $this->query($sql);

    if(MYSQL_NUM_ROWS($result) == 0)
      $value = false;
    else
      $value = MYSQL_RESULT($result, 0);
      return $value;
    }
} 

?>
  • 写回答

1条回答 默认 最新

  • dongzhaiqiang6108 2014-01-29 11:31
    关注

    The clash on the query method was probably catching you, I've renamed this _query as you might noticed. I've tested each method to ensure they work also. Let me know if you have any issues.

    class DB_Class {
    
        //database selection classes
        private $db;
    
        function DB_Class($dbname, $host, $username, $password) {
            $this->db = new mysqli($host, $username, $password, $dbname);
        }
    
        function _query($sql) {
            $result = $this->db->query($sql) or die ("Invalid query: " . mysqli_error());
            return $result;
        }
    
        function fetch($sql) {
            $data = array();
            $result = $this->_query($sql);
    
            while($row = mysqli_fetch_array($result)) {
                $data[] = $row;
            }
            return $data;
        }
    
        function getone($sql) {
            $result = $this->_query($sql);
    
            if(mysqli_num_rows($result) == 0)
                $value = false;
            else
                $value = $result->fetch_row();
    
            return $value;
        }
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大