2016-07-19 27 views
0

私はmysqlデータベースにデータを挿入しようとしています。 私はこのerrrorを取得しかし、クラスの接続PHP OOP query()error - 未定義のメソッドconnection :: query()を呼び出す

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; 
    }  
} 
} 

そして私は、データベースにデータを挿入しようとする魔女内の別のクラスは

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

(私は、そのクラスの__constructでこれをやろう)があります。

Fatal error: Call to undefined method connection::query() in ....

+0

クラス接続にはquery()メソッドがありません。 __construct()メソッド内に作成されたmysqliオブジェクトには、次のようなものがあります。 –

+3

エラーはかなりわかりやすいです。 'connection'クラスに' query'という名前のメソッドはありません。 – mituw16

+0

公式のPHPドキュメント[http:// php。 net/manual/en/mysqli.query.php]私はその機能が組み込まれていることを理解しました。だから私はそれについて間違っている? @ CT14.IT –

答えて

2

クエリ(mysqliクラスから)メソッドを使用するには、新しいメソッドpublic function query() {}を作成する必要があります。このメソッドはmysqliメソッドを使用できます。

<?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; 
    } 

} 

メソッドを呼び出す:最後に、あなたはここでとても$result = $db->query($sql);

のように独自のオブジェクト($デシベル)で「クエリ」を適用しても同じ結果をacheiveすることができるようになりますと、クラスの

<?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); 
+0

'$ this-> db'または' $ this-> conn'ですか? – tadman

+0

申し訳ありませんが、メソッドクエリでは '$ this-> conn'です。私は自分の投稿を編集しています... – Ivan

+0

@ivanその仕事。私はこの部分を理解しています: '$ this-> conn-> query($ sql);' query()関数から既にmysqliクラスが正しく呼び出されていますか? –

0

最後の行には$result = $db->query($sql);があります。あなたは 'query'という名前の関数を呼び出そうとしています。接続クラスを調べる場合、唯一の機能はコンストラクタです。

これを修正するには、「クエリ」という名前の関数を追加する必要があります(関数の理想的な名前ではありません)。ここで

は、私はあなたがまっすぐにPHPのデータ・オブジェクトに飛び込むとPDOがに最高の「標準的な」方法であるので、それを使って行うことをお勧めしますいくつかのコメントコード(エラーがないことを保証しません!)

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); 

です最近アクセスデータベース。

関連する問題