I have following code, however $email is not getting printed on page. I don't know why.
<?php
$email = "myemil#gmail.com";
$tag = "login";
require_once 'DB_Functions.php';
$db = new DB_Functions();
echo $email;
?>
If I remove
require_once 'DB_Functions.php'; $db = new DB_Functions();
then it prints email on page. Why is this happening? I want to connect to DB. Please help.
Above code file is on godaddy server, in a subdomain.
DB_Functions.php looks like
class DB_Functions {
private $db;
function __construct() {
require_once 'DB_Connect.php';
$this->db = new DB_Connect();
$this->db->connect();
}
function __destruct() {
}
}
DB_Connect looks like
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
require_once 'config.php';
// connecting to mysql
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
// selecting database
mysql_select_db(DB_DATABASE);
// return database handler
return $con;
}
// Closing database connection
public function close() {
mysql_close();
}
}
config.php
define("DB_HOST", "localhost");
define("DB_USER", "db1");
define("DB_PASSWORD", "db1");
define("DB_DATABASE", "db1");
Related
in my php i call database connection from other php but i got confuse here its not echo current php echo
this my db.php code
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
class Database {
private $con;
public function connect (){
include_once("constant.php");
$this->con = new Mysqli(HOST,USER,PASS,DB);
if ($this->con->connect_error) {
echo"connect fails";
//return $this->con;
}else{echo "connection success";}
//return "DATABASE_CONNECTION_FAIL";
}
}
$db = new Database();
$db->connect();
?>
and this my user.php code
<?php
/**
* user class for account creation and login purpose
*/
class User {
private $con;
function __construct(){
include_once("../database/db.php");
$db = new Database();
$this->con = $db->connect();
if($this->con) {
echo "connect databases";
}
}
}
$obj = new User();
?>
in my browser i call user.php but i got echo as connection success connection success its repeat 2 time but in user.php echo not display
my expect echo is connect databases
This line in your user.php calls the connect() function in your Database class. This also echoes the first "connection success" string:
$this->con = $db->connect();
This line in your user.php calls again the connect() function in your Database class, and prints the second "connection success" string.
if($this->con) { ... }
These lines in your user.php does not work because connect() function does not return anything:
if($this->con) {
echo "connect databases";
}
You can replace your echoes in your Database class if-else block with return statements like this if you only want the "connect databases" string result:
if ($this->con->connect_error) {
return false;
} else {
return true;
}
After your connection is success. you should return the connection.
try this below db.php code
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
class Database {
private $con;
public function connect() {
include_once("constant.php");
$this->con = new Mysqli(HOST, USER, PASS, DB);
if ($this->con->connect_error) {
echo"connect fails";
}
else {
// return successful connection
return $this->con;
}
}
}
$db = new Database();
$db->connect();
?>
I am new to working with PHP and Mysql Database.
I am using MAMP's phpmyadmin to create database.
By below code works fine and establishes connection with the server:
<?php
include "config.php";
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
?>
But, i have a problem with connecting when i use constructor and a class :
Config.php:
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "scapik_users");
define("DB_PORT","8889");
?>
DbConnect.php
<?php
include "config.php";
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
?>
Could someone help me correct the code, I guess some where i am going wrong.
Thanks.
create object of class
$obj = new DbConnect();
then run your code
<?php
include "config.php";
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
} else {
echo "connection successfull";
}
}
public function getDb(){
return $this->connect;
}
}
$obj = new DbConnect();
?>
may be it will help you.
Take adavantage of Object oriented programming. Pass your databse paramters when initializing the DbConnect class. This would help you use the initialized variables throughout this class as well.
<?php
include "config.php";
class DbConnect{
private $connect;
private $host;
private $username;
private $password;
private $database;
public function __construct($host, $username, $password, $database){
$this->host = $host;
$this->username= $username;
$this->password = $password;
$this->database = $database;
$this->connect = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
?>
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "scapik_users");
define("DB_PORT","8889");
?>
<?php
$host = DB_HOST;
$username = DB_USERNAME;
$password = DB_PASSWORD;
$database = DB_DATABASE;
#call this class as:
$class = new DbConnect($host, $username, $password, $database);
?>
Try adding your port information in the constructor. Since you defined it, it may be needed to connect to the database, as it may only be listening on that port.
EDITED my answer with full code example - this works on my side
config.php
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "test");
define("DB_PORT","3306");
DBConnect.php
<?php
include("config.php");
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
$db = new DbConnect();
print_r($db->getDb());
?>
This works fine on my side... So I guess your code is fine.
I guess you have already checked your user credentials and the port and stuff... ?
Did you try setting your error reporting?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
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();
}
}
?>
I am a beginner in PDO. I am following a tutorial to learning PDO. I want to use a simple select statement to fetch id of users.
but when I run index.php it dont show any echo ! where is my wrong ?
I have four files :
config => setting username and password...
DB_Connect :
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
require_once 'include/config.php';
try {
$hostname = DB_HOST ;
$dbname = DB_DATABASE;
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
return $dbh;
}
}
DB_Functions :
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
function getUsers(){
$sql = "SELECT * FROM users";
foreach ($this->$db->query($sql) as $row)
{
echo $row->id;
}
/*** close the database connection ***/
// $db = null;
}
}
index.php
<?php
require_once 'include/DB_Functions.php';
$qr = new DB_Functions();
$qr->getUsers();
?>
db_connect
class DB_Connect {
public $dbh;
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
require_once 'include/config.php';
try {
$hostname = DB_HOST ;
$dbname = DB_DATABASE;
$this->dbh = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
db_functions
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
function getUsers(){
$sql = "SELECT * FROM users";
foreach ($this->db->dbh->query($sql) as $row)
{
echo $row->id;
}
/*** close the database connection ***/
// $db = null;
}
}
You have no database connection because you're now assigning your PDO connection to a variable. So you're connection is not accessible to the rest of your script. At least, that's what I'm thinking at the moment.
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()