【问题标题】:PHP OOP and MySQLi connection = Fatal error: Call to undefined method mysqli::arrayQuery()PHP OOP 和 MySQLi 连接 = 致命错误:调用未定义的方法 mysqli::arrayQuery()
【发布时间】:2015-06-04 06:47:21
【问题描述】:

请告诉我我做错了什么?以及如何更好地与基地合作?连接有效,但我看不到来自基地的信息。我刚刚得到:

致命错误:调用未定义的方法 mysqli::arrayQuery()

我不明白如何解决它,Google 也没有提供帮助。

<?php
class Proc{
    protected $DB;
    function __construct(){
        $this->DB=new mysqli('localhost', 'user', 'password', 'basename');
        $this->DB->query("set names utf8");}
    function __destruct(){unset($this->DB);}
    function GetAll(){
        $sql="SELECT * FROM users";
        $result = $this->DB->arrayQuery($sql, SQLITE_ASSOC);
        return $result;}
}

$Yo = new Proc();

$users = $Yo->GetAll();
echo "All users: ".count($users);
foreach ($users as $user){
    $id = $user["ID"];
    $n = $user["Name"];
    echo "{$id} - {$n}<br/>";}
?>

稍作修复,一切正常!谢谢大家!

<?php
class Proc{
    protected $DB;
    function __construct(){
        $this->DB=new PDO("mysql:host=localhost;dbname=basename", user, password);
        $this->DB->query("set names utf8");}
    function __destruct(){unset($this->DB);}
    function GetAll(){
        $sql="SELECT * FROM users";
        $result = $this->DB->query($sql);
        return $result;}
}
$Yo = new Proc();
$users = $Yo->GetAll();
foreach ($users as $user){
    $id = $user["ID"];
    $n = $user["Name"];
    echo "{$id} - {$n}<br/>";}
?>

【问题讨论】:

  • 调用未定义的方法 mysqli::arrayQuery() - 我的意思是错误信息很清楚,不是吗?
  • 有这个功能可以发arrayQuery
  • 检查php.net/manual/en/book.mysqli.php 显示arrayQuery 不可用(mysqli 类没有这样的方法)。另外,切换到 PDO 可能会更好

标签: php mysql oop pdo mysqli


【解决方案1】:

您使用的是什么数据库? SQLitemysql ?

因为按照PHP DOCS,我猜arrayQuery这个函数只能用于SQLite数据库

【讨论】:

  • MySQL 5.5.37,并以“mysqli”身份连接。所以这不是 SQLite?
  • 不……那是你拥有的mysql数据库,因为你已经初始化了“mysqli”,所以mysqli类没有那个方法
猜你喜欢
  • 2023-04-07
  • 2012-08-10
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-07
相关资源
最近更新 更多