简体   繁体   中英

Undefined variable: con in C:\wamp\www\android_connect\db_connect.php on line <i>48</i>

In my Android App. I'm trying to authenticate the user, for that I have written a php script to establish connection to the database but unfortunately I'm getting the following error--

"Undefined variable: con in C:\\wamp\\www\\android_connect\\db_connect.php on line 48 "

To resolve this error I have declared "$con" as global variable but still getting the same error-

My db_connect.php code is---

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

    }   
}

?>

Please Help. Thank you..!

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

?>

Use $this->con , instead of $con , as $con is a class variable, and you are using as a local variable.

Problem - $con variable in function connect() is not assigning value of $con to the public variable you have given, and similarly, in close() function it was not referencing to the public variable.

Change your function connect as -

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

and function close() as -

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

    } 

Final Code -

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

    }   
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM