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
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.
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
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();
}
}
?>
Related
I am very new in PHP and I'm trying to connect to my mysql database.
The code for select is the following:
public function getAllTeams(){
$stmt = $this->conn->prepare("SELECT * FROM maras_app");
$stmt->execute();
$result = $stmt->get_result();
echo json_encode($result);
return $result;
}
The connection is successful because I put in code to echo if there is an error. The response I get is the following:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
The tables have 14 columns.
In the dataBase I have only one row inserted with last 4 columns null.
Any idea why I'm getting null as response?
This is how I'm creating my connection:
private $conn;
//Constructor
function __construct()
{
require_once dirname(__FILE__) . '/Configuratii.php';
require_once dirname(__FILE__) . '/ConexiunDB.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
Here is Configuratii.php:
define('DB_USERNAME', '---');
define('DB_PASSWORD', '---');
define('DB_HOST', '---');
define('DB_NAME', '---');
And here is ConexiunDB.php:
class DbConnect
{
private $conn;
function __construct()
{
}
function connect()
{
require_once 'Configuratii.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// returing connection resource
return $this->conn;
}
}
I really can't get what's there and don't know to debug because is the second time I use PHP.
DbConnect.php
<?php
/**
* Handling database connection
*/
include_once ('Config.php');
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* #return database connection handler
*/
function connect() {
// Connecting to mysql database
$this->conn = new mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// returing connection resource
return $this->conn;
}
}
class DbHandler {
private $conn;
function __construct() {
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}}
?>
config.php
<?php
/**
* Database configuration
*/
define('DB_USERNAME', 'medma');
define('DB_PASSWORD', 'medma');
define('DB_HOST', 'localhost');
define('DB_NAME', 'medma_sameer');
/**
* MSG91 configuration
*/
define('MSG91_AUTH_KEY', "130558AmTMgKzL582187bb");
// sender id should 6 character long
define('MSG91_SENDER_ID', 'MEDMA1');
define('USER_CREATED_SUCCESSFULLY', 0);
define('USER_CREATE_FAILED', 1);
define('USER_ALREADY_EXISTED', 2);
?>
Please tell me what could be the problem though i have posted before Not able to hit api to verify otp ( Used Volley)
but when i started testing each php file on my localhost folder where i have mounted i got stuck with the very initial step which is database connection the code i wrote above doesn't show anything except blank
There are 2 problems here that could cause the page to be blank:
The first is that you don't actually output anything in your script, at least not in the code you have posted.
The bigger problem is that this is probably wrong as you don't seem to be inside a method / class:
$db = new DbConnect();
$this->conn = $db->connect();
$this is defined in the scope of a method so using it like you are doing would lead to a fatal error:
Fatal error: Using $this when not in object context in ...
As a side-note: If you want to show the contents of a variable, you should use var_dump() instead of echo because the last only gives any meaningfull output for numbers and strings; not for database connections.
First of all you bad invoke a connection here
$db = new DbConnect();
$this->conn = $db->connect();
You can not access property conn in this scope (outside of a class) this way
You can do this using $db->conn. However in this situation you can't access it aswell as it is a private
If you would like to do this using your class you can try
$db = new DbConnect();
$conn = $db->connect();
and under $conn u have a db connection.
#edit:
Maybe try use this
<?php
include_once ('Config.php');
/**
* Handling database connection
*/
class DbConnect {
private static $_instance; //The single instance
private $_conn;
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
// Connecting to mysql database
$this->_conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
}
// Get connection
function getConnection() {
return $this->_conn;
}
}
To make a connection to database you can do that
$db = DbConnect::getInstance();
$conn = $db->getConnection();
$query= "SELECT foo FROM .....";
$result = $conn->query($query);
I am working on the project in which i am supposed to connect my android application to phpMyAdmin database but there is some problem. As I run the code, same code appears on the browser.
I am doing everyting else correctly like connections etc.
Here is my code, someone please explain what is the problem
CODE:
'
/** * 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();
}
}
?>'
you should put <?php to the beginning of a file
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);
}
}
?>
I'm trying to connect to the database, but dont know what is the problem with this one. the code goes like this. Please help I'm very new to the php. :(
db_config.php
<?php define('DB_USER', "root");
define('DB_PASSWORD', "database password");
define('DB_DATABASE', "ews_app");
define('DB_SERVER', "Im using the 'drupal' for webhosting should I give server address or localhost");
?>
db_connect.php
<?php
class DB_CONNECT {
function __construct() {
$this->connect();
echo "<p>server connected</p>";
}
function __destruct() {
$this->close();
}
function connect() {
require_once __DIR__ . '/db_config.php';
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
return $con;
}
function close() {
mysql_close();
}
}
?>
It should print server connected once it connect to the database. Are there any modification needed on it.
You don't appear to ever be instantiating the object, so the constructor never gets run, so of course no connection is established, so you don't get any output.
Try adding $connection = new DB_CONNECT()