php 致命错误:调用未定义的函数 mysqli()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13170672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:53:37  来源:igfitidea点击:

Fatal error: Call to undefined function mysqli() in

phpmysqli

提问by Rasim Fryal

I am instentiating $db object this way in my database class

我在我的数据库类中以这种方式实例化 $db 对象

class db{
private $connection;

public function __construct(){
    $this->connect();
    }
public function connect(){
    $this->connection=mysqli_connect('localhost','root','','new5');
    }
   }
        $db=new db;

I am using this $db object in another class(profile class) as link identifier as

我在另一个类(配置文件类)中使用这个 $db 对象作为链接标识符

   $query=$db->query("SELECT id, parent_id, name FROM categories_general");

Please help me ,how can i get rid of this error.Actually I am recently shifted from mysql to mysqli.

请帮助我,我怎样才能摆脱这个错误。实际上,我最近从 mysql 转移到了 mysqli。

回答by case1352

try

尝试

$db = mysqli_connect('localhost','root','XXX','XXX');

回答by Xingjia Luo

$db = new mysqli('localhost','root','','test');
$sql = "select * from user";
$result = $db->query($sql);
var_dump($result->fetch_assoc());

回答by zenon

You are using procedural style of mysqli. So you must create query method in db class.

您正在使用 mysqli 的程序风格。所以你必须在 db 类中创建查询方法。

class db{
  private $connection;

  public function __construct(){
    $this->connect();
  }
  public function connect(){
    $this->connection=mysqli_connect('localhost','root','','new5');
  }
  //add these methods
  public function query($query_string){
    return mysqli_query($this->connection, $query_string);
  }
  public function fetch_row($mysqli_result){
    return mysqli_fetch_row($this->connection, $mysqli_result);
  }
  //and other functions you need
}

or you can use object oriented style with extends method;

或者您可以使用带有扩展方法的面向对象样式;

class db extends mysqli{
  function __construct($host, $username, $password, $databaseName, $port=3306){
    mysqli::__construct($host, $username, $password, $databaseName, $port);
  }
}

回答by Xingjia Luo

add a function

添加功能

public function getDB()
{
    return $this->_mysql;
}

 $query=mysqli_query($db->getDB(),"SELECT id, parent_id, name FROM categories_general");