PHP OOP query()错误 - 调用未定义的方法连接:: query()

时间:2016-07-19 16:16:49

标签: php mysql oop

我试图将数据插入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);

然而我得到了这个错误:

  

致命错误:在....中调用未定义的方法连接:: query()

2 个答案:

答案 0 :(得分:2)

为了使用查询(来自mysqli类)方法,您需要创建一个新方法public function query() {}。这个方法将能够使用mysqli方法。最后,您将能够实现相同的结果,但是在您自己的对象($ db)上应用'query',如此$result = $db->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);

答案 1 :(得分:1)

因为在最后一行 - $result = $db->query($sql); - 您正在尝试调用名为&#39; query&#39;的函数。如果你查看你的连接类,你唯一的功能就是构造函数。

要解决此问题,您需要添加一个名为&#34; query&#34;的函数。 (注意这不是函数的理想名称。)

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

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是最好的&#39;标准&#39;这些天访问数据库的方法。