2014-07-02 55 views
0

在我的Android應用程序中。我試圖驗證用戶,因爲我寫了一個PHP腳本來建立連接到數據庫,但不幸的是我得到以下錯誤 -未定義變量:con在C: wamp www android_connect db_connect.php在線

「Undefined variable:con in C:\ wamp \ WWW \ android_connect \ db_connect.php上線「

爲了解決這個錯誤我已經宣佈」$ CON「爲全局變量,但仍然得到同樣的錯誤 -

db_connect.php代碼---

<?php 

/** 
* A class file to connect to database 
*/ 
class DB_CONNECT 
{ 

    private $con;  

    // constructor 
    function __construct() 
    { 
     // connecting to database 
     $this->connect(); 
    } 

    // destructor 
    function __destruct() 
    { 
     // closing db connection 
     $this->close(); 
    } 

    /** 
    * Function to connect with database 
    */ 
    function connect() 
    { 
     // import database connection variables 
     require_once __DIR__ . '/db_config.php'; 

     // Connecting to mysql database    
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 

     // Selecing database 
     $db = mysqli_select_db($con, DB_DATABASE); 
     // returing connection cursor 
     return $con; 
    } 

    /*  
    * Function to close db connection 
    */ 
    function close() 
    { 
    // closing db connection 
     mysqli_close($con); 

    } 
} 

?> 

請幫助。謝謝..!

+1

您需要參考'$這個 - > con' –

回答

0
<?php 

/** 
* A class file to connect to database 
*/ 
class DB_CONNECT 
{ 

    private $con;  

    // constructor 
    function __construct() 
    { 
     // connecting to database 
     $this->connect(); 
    } 

    // destructor 
    function __destruct() 
    { 
     // closing db connection 
     $this->close(); 
    } 

    /** 
    * Function to connect with database 
    */ 
    function connect() 
    { 
     // import database connection variables 
     require_once __DIR__ . '/db_config.php'; 

     // Connecting to mysql database    
     $this->con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 

     // Selecing database 
     $db = mysqli_select_db($this->con, DB_DATABASE); 

     // returing connection cursor 
     return $this->con; 
    } 

    /*  
    * Function to close db connection 
    */ 
    function close() 
    { 
     // closing db connection 
     mysqli_close($this->con); 
    } 
} 

?> 

使用$this->con,而不是$con,作爲$con是一個類變量,並且使用的是作爲一個局部變量。

+0

正如你所建議的我用這個 - >騙我甚至複製你的答案,但仍然無法正常工作..! – Namo

+0

@Namo,請再次嘗試使用上面的代碼,因爲我忘記返回'$ this-> con',而是返回'$ con'。如果仍然無效,請提及,如果您收到與第48行中的未定義變量:con在C:\ wamp \ www \ android_connect \ db_connect.php中相同的錯誤,請執行以下操作: – prava

+0

太棒了!爲我工作..非常感謝Prava ..! – Namo

0

問題 - 在功能connect()$con變量未分配的$con價值,你給公共變量,同樣,在close()功能它沒有引用到公共變量。

更改功能連接爲 -

function connect() 
    { 
     // import database connection variables 
     require_once __DIR__ . '/db_config.php'; 

     // Connecting to mysql database    
    $this->con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 

     // Selecing database 
     $db = mysqli_select_db($con, DB_DATABASE); 
     // returing connection cursor 
     return $con; 
    } 

和功能接近()作爲 -

function close() 
    { 
    // closing db connection 
     mysqli_close($this->con); 

    } 

終極密碼 -

<?php 

/** 
* A class file to connect to database 
*/ 
class DB_CONNECT 
{ 

    private $con;  

    // constructor 
    function __construct() 
    { 
     // connecting to database 
     $this->connect(); 
    } 

    // destructor 
    function __destruct() 
    { 
     // closing db connection 
     $this->close(); 
    } 

    /** 
    * Function to connect with database 
    */ 
    function connect() 
    { 
     // import database connection variables 
     require_once __DIR__ . '/db_config.php'; 

     // Connecting to mysql database    
    $this->con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE); 

     // Selecing database 
     $db = mysqli_select_db($con, DB_DATABASE); 
     // returing connection cursor 
     return $con; 
    } 

    /*  
    * Function to close db connection 
    */ 
    function close() 
    { 
    // closing db connection 
     mysqli_close($this->con); 

    } 
} 

?> 
+0

正如你所建議我用這個 - >騙我甚至複製你的答案,但仍然無法正常工作..! – Namo

+0

它仍然給出相同的錯誤? – Anik

+0

是的..仍然有同樣的錯誤..!\ – Namo

相關問題