【问题标题】:PHP OOP query() error - Call to undefined method connection::query()PHP OOP query() 错误 - 调用未定义的方法 connection::query()
【发布时间】:2016-07-19 16:16:49
【问题描述】:

我正在尝试将数据插入 mysql 数据库。 我有班级联系

    class connection{
function __construct(){
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $database='products2';

    // Create connection
    $conn = new mysqli($servername, $username, $password,$database);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        return false;
    }else{
        echo "succesful connection!</br>";
        $this->query=$conn;
        return true;
    }       
}
}

另一个类我尝试将数据插入数据库(我尝试在该类的 __construct 中执行此操作)

$sql="INSERT INTO products (name,price,category,f_material,f_size) VALUES ('nosaukums','cena','kategorija,'materials','izmers')";
$db=new connection();
$result=$db->query($sql);

但是我得到了这个错误:

致命错误:调用未定义的方法 connection::query() in ....

【问题讨论】:

  • 类连接,没有query()方法。在 __construct() 方法中创建的 mysqli 对象确实有这个,
  • 这个错误很容易解释......在你的connection 类中没有名为query 的方法
  • 查看官方 php 文档 [php.net/manual/en/mysqli.query.php] 我知道它是一个内置函数。所以我错了? @CT14.IT
  • 不,您并没有错,但是...您的connection 班级没有此方法。 mysqli 类可以。当您调用 $db-&gt;query() 时,您正在尝试调用 connection 类上的方法,这显然不存在。

标签: php mysql oop


【解决方案1】:

为了使用查询(来自mysqli 类)方法,您需要创建一个新方法public function query() {}。这个方法就可以使用mysqli方法了。最后,您将能够获得相同的结果,但通过在您自己的对象 ($db) 上应用“查询”,就像 $result = $db-&gt;query($sql);

这是课程:

<?php

class connection{

  // define a variable for the database connection
  private $conn;

  // when an instance of 'connection' class is created a connection is made through mysqli
  public function __construct()
  {
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $database='products2';

    // the connection is stored inside the private variable
    $this->conn = new mysqli($servername, $username, $password,$database);

    // Check connection
    if ($this->conn->connect_error) {
        die("Connection failed: " . $this->conn->connect_error);
        return false;
    } else{
        echo "succesful connection!</br>";
        return true;
    }       
  }

  // method used to send a query to database
  public function query($sql)
  {
    // here you use the connection made on __construct() and apply the method query. Basically we are using inside our method (called query) a method (call query too) from the mysqli class
    $this->conn->query($sql);
    return true;
  }

}

调用方法:

<?php

    // prepare the SQL request
    $sql = "INSERT INTO products (name,price,category,f_material,f_size) VALUES ('nosaukums','cena','kategorija,'materials','izmers')";

    // create a new instance of the class connection
    $db = new connection();

    // run the method $sql on the newly created instance $db
    $result = $db->query($sql);

【讨论】:

  • $this-&gt;db$this-&gt;conn?
  • @ivan 它的工作。据我了解这部分:来自 query() 函数的$this-&gt;conn-&gt;query($sql); 已经调用了 mysqli 类吗?
  • 是的!我在这部分代码的上方添加了一条注释:我们在我们自己的class connection 中使用class mysqli 中的查询方法。这就是您可以在自己的类中使用该方法的方式...
【解决方案2】:

因为在你的最后一行 - $result = $db-&gt;query($sql); - 你试图调用一个名为“查询”的函数。如果您查看连接类的内部,那么您拥有的唯一功能就是构造函数。

要解决此问题,您需要添加一个名为“查询”的函数(请注意,这不是该函数的理想名称)。

这是一些注释代码(不保证没有错误!)

class connection{

    protected $conn; // add conn so that you can use it later

    function __construct()
    {
        $servername = 'localhost';
        $username = 'root';
        $password = '';
        $database='_mvc';

        // Assign $this->conn to a database object
        $this->conn = new mysqli($servername, $username, $password, $database);

        // Remember we are checking >>THIS<< conn now
        if ($this->conn->connect_error) {
            die("Connection failed: " . $this->conn->connect_error);
            return false;
        }else{
            // no need to assign anthing here as the database object has already been assigned to $this->conn
            echo "succesful connection!</br>";
            return true;
        }
    }

    // here is the missing function
    public function query($sql) {
        // now we are accessing the database objects query method and massing the SQL
        return $this->conn->query($sql);
    }
}

$sql =
"INSERT INTO products (name,price,category,f_material,f_size)
 VALUES ('nosaukums','cena','kategorija,'materials','izmers')";

$db = new connection();
$result = $db->query($sql);

我建议你直接进入 php 数据对象并完成它,因为 PDO 是当今访问数据库的最佳“标准”方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多