douzao9845
2014-02-02 18:11
浏览 79
已采纳

警告:mysqli_query()期望参数1为mysqli [关闭]

I get this error when I try my code:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in D:\AppServ\www\my\classes\db.class.php on line 23

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in D:\AppServ\www\my\classes\db.class.php on line 23

db.class.php file:

class DBclass{
    private $servername;
    private $username;
    private $password;
    private $dbname;
    private $connect;

    public function connectDB(){

        $this->connect = mysqli_connect($this->servername,$this->username,$this->password,$this->dbname);

        if(!$this->connect){
            echo 'Error : Fail to connect to database !!' . mysqli_error($this->connect);
        }
    }
    public function selectTBL($colum,$from,$where = '',$value = ''){
        if($where == '' && $value == ''){
            $query = 'SELECT '.$colum.'FROM '.$from;
        }elseif($where != '' && $value != ''){
            $query = 'SELECT '.$colum.'FROM '.$from.' WHERE '.$where. ' = '.$value;
        }
        mysqli_query($query,$this->connect) or die(mysqli_error($this->connect));
        $row = mysqli_fetch_array($query);
        return $row[$colum];
    }
}

home.html file:

<?php  $query = new DBclass; ?>
<html dir="rtl">
    <head>
        <title>title</title>
    </head>
    <body>
        <?php $query->selectTBL('VALUE','SITE_CONFIGS','CONFIG','SITE_TITLE'); ?>
    </body>
</html>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doufen1890 2014-02-02 18:15
    最佳回答
        public function selectTBL($colum,$from,$where = '',$value = '')
        {
            if( is_null($this->connect))
                $this->connectDB();
    
            if($where == '' && $value == ''){
                $query = 'SELECT '.$colum.'FROM '.$from;
            }elseif($where != '' && $value != ''){
                $query = 'SELECT '.$colum.'FROM '.$from.' WHERE '.$where. ' = '.$value;
            }
            $result = mysqli_query($this->connect, $query) or die(mysqli_error($this->connect));
            $row = mysqli_fetch_array($result);
            return $row[$colum];
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题