-6

Currently My code is this to connect my database.

<?php

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

// 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 = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>

db_config.php is

<?php

define('DB_USER', "root"); // db user 
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "leading10"); // database name
define('DB_SERVER', "localhost"); // db server
?>

on using the above code somewhere it shows an warning to use mysqli or PDO I followed many stackoverflow questions but still I was not able to do it successfully . One of the methods I know is to use @ symbol before mysql_connect() but it may not work in future. Any Help Appreciated

3 Answers3

1

As other guys already wrote you should use mysqli_* functions. But this is not the problem in your case.

The problem is you assign the connection to $con which is a local variable and therefore - once connect() quits - the value gets lost.

This code works:

class DB_CONNECT
{
    private $connection;
    private $database;

    function __construct() {
        $this->connect();
    }

    function __destruct() {
        $this->close();
    }

    function connect() {
        $this->connection = mysqli_connect( DB_SERVER, DB_USER, DB_PASSWORD ) or die(mysql_error());
        $this->database = mysqli_select_db( DB_DATABASE ) or die(mysql_error());
    }

    function close() {
        mysqli_close( $this->connection );
    }
}

Note:

In your code you have twice or die(...) in a line.

Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
-1

Use this to turn off error reporting

write it on the top of the page

error_reporting(E_ERROR | E_WARNING | E_PARSE);

here is the link for more info

http://www.w3schools.com/php/func_error_reporting.asp

-1

It is not working because you are using mysql, which is depreciated, change all mysql to mysqli. This is an edited version of your code.

 <?php

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

// 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) or die(mysqli_error());

    // Selecing database
    $db = mysqli_select_db(DB_DATABASE) or die(mysqli_error());

    // returing connection cursor
    return $con;
}

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

}

?>
Ukor
  • 322
  • 4
  • 16