2015-09-27 69 views
0

有人可以解釋嗎有人可以解釋爲什麼它不工作?

爲什麼這不起作用?

<?php 
class category 
{ 
    function __construct() 
    { 
     $con = new mysqli("localhost", "root", "", "whatever"); 
    } 

    function show_all() 
    { 
     $sql = "SELECT id_kategori, nama_kategori FROM kategori"; 
     $stmt = $con->prepare($sql); 
     $stmt->execute(); 
     $stmt->bind_result($id, $cat); 
     while($stmt->fetch()) 
     { 
      echo "<td>$id</td>"; 
      echo "<td>$cat</td>"; 
      echo "<td>Update</td>"; 
      echo "<td>Delete</td>"; 
     }; 
     $stmt->close(); 
    } 

} 
?> 

但是,這項工作?

<?php 
class category 
{ 

    function show_all() 
    { 
     $con = new mysqli("localhost", "root", "", "whatever"); 
     $sql = "SELECT id_kategori, nama_kategori FROM kategori"; 
     $stmt = $con->prepare($sql); 
     $stmt->execute(); 
     $stmt->bind_result($id, $cat); 
     while($stmt->fetch()) 
     { 
      echo "<td>$id</td>"; 
      echo "<td>$cat</td>"; 
      echo "<td>Update</td>"; 
      echo "<td>Delete</td>"; 
     }; 
     $stmt->close(); 
    } 

} 
?> 

沒有構造,它的工作,與構造它不。

有人可以告訴我,告訴我,教我如何在一個構造中包含sql連接的正確方式?我還是新的,並且順便學習。

+2

可能是因爲它已經是一個構造http://php.net/manual/en/mysqli.construct.php –

+1

雖然這在技術上是正確的,但OP應該知道爲什麼特定的範圍變量很重要,因爲看起來他們想學習如何構造 – Jesse

回答

3

這是因爲範圍界定。 $con變量應定義爲專門用於該類,而不僅僅侷限於__construct

當你定義$con__construct裏面你是觀察這個在本地使用的功能__construct內,而不是在類本身

考慮下面的代碼

<?php 
class category 
{ 
    private $con; 

    function __construct() 
    { 
     $this->con = new mysqli("localhost", "root", "", "whatever"); 
    } 

    function show_all() 
    { 
     $sql = "SELECT id_kategori, nama_kategori FROM kategori"; 
     $stmt = $this->con->prepare($sql); 
     $stmt->execute(); 
     $stmt->bind_result($id, $cat); 
     while($stmt->fetch()) 
     { 
      echo "<td>$id</td>"; 
      echo "<td>$cat</td>"; 
      echo "<td>Update</td>"; 
      echo "<td>Delete</td>"; 
     }; 
     $stmt->close(); 
    } 

} 
?> 
+0

添加「this 「給我錯誤結果 Noti c:\ xampp \ htdocs \ whatever \ login \ admin \ sql \ category.php on line 6 致命錯誤:無法訪問C:\ xampp \ htdocs \ whatever \ login \ admin中的空屬性\ sql \ category.php on line 6 :( – user2977799

+1

請參閱完整的代碼,我將變量定義爲私有類 – Jesse

+0

等待,它在我複製後粘貼它們:D多個thnx:D – user2977799

2

$ con是不是一個變量在上面的類訪問:試試這個:

<?php 
class category 
{ 

    private $con = NULL; 

    function __construct() 
    { 
     $this->con = new mysqli("localhost", "root", "", "whatever"); 
    } 

    function show_all() 
    { 
     $sql = "SELECT id_kategori, nama_kategori FROM kategori"; 
     $stmt = $this->con->prepare($sql); 
     $stmt->execute(); 
     $stmt->bind_result($id, $cat); 
     while($stmt->fetch()) 
     { 
      echo "<td>$id</td>"; 
      echo "<td>$cat</td>"; 
      echo "<td>Update</td>"; 
      echo "<td>Delete</td>"; 
     }; 
     $stmt->close(); 
    } 

} 
?> 

看看這裏的文檔: http://php.net/manual/en/language.oop5.php 和查找PHP範圍: http://php.net/manual/en/language.variables.scope.php

此外,如果您有問題,總是添加到您的代碼: 這會告訴你的變量是不確定的:

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
+0

這是正確的。 Thnx的答案。 :) – user2977799

相關問題