drtpbx3606 2014-01-29 03: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 03: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;
        }
    }    
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部