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;
        }
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面