I am working on XYZ project. In that project having admin, assets, class, controller, core folder and some .php files. In core folder having config.php and database.php files.
Here's config.php code:
<?php
include ("core/database.php");
spl_autoload_register(function ($class_name) {
include ('class/'.$class_name . '.php');
});
?>
and database.php code:
<?php
session_start();
define('BASE_URL', 'http://localhost/XYZ/');
class database{
private $servername;
private $username;
private $password;
private $dbname;
protected function connect(){
$this->servername = 'localhost';
$this->username = 'root';
$this->password = '';
$this->dbname = 'xyz';
$conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
return $conn;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
}
}
?>
Here's index.php code:
<?php
include("core/config.php");
echo BASE_URL;
exit();
?>
In that page i got BASE_URL in defined in database.php
But when i try to get same BASE_URL in admin->index.php got some error following:
Warning: include(core/database.php): failed to open stream: No such file or directory in C:\xampp\htdocs\xyz\core\config.php on line 3
Warning: include(): Failed opening 'core/database.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\xyz\core\config.php on line 3
Notice: Use of undefined constant BASE_URL - assumed 'BASE_URL' in C:\xampp\htdocs\xyz\admin\index.php on line 3
BASE_URL
So, My query is how to access databse connection and BASE_URL in admin->index.php
#rajeev answer i got database connection and BASE_URL in admin->index.php
But i have one more query
In class folder having Test.php file the code is below:
<?php
class Test extends Database{
function __construct(){
date_default_timezone_set('Asia/Kolkata');
$date = date("y-m-d H:i:s");
$this->get_date = $date;
}
public function test(){
return $this->get_date;
}
}
?>
This class i want access in admin->index.php file but get error below
Warning: include(class/Test.php): failed to open stream: No such file or directory in C:\xampp\htdocs\technical\core\config.php on line 6
Warning: include(): Failed opening 'class/Test.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\technical\core\config.php on line 6
Fatal error: Class 'Test' not found in C:\xampp\htdocs\technical\admin\index.php on line 5
and i tried code in admin->index.php
<?php
include("../core/config.php");
$testClass = new Test();
echo $testClass->test();
?>
You can use "getcwd"(https://www.php.net/manual/en/function.getcwd.php) function to gets the current working directory. If the "class" folder isn't accessible in the current working directory, you will have to change your include path in the "config.php" file.
Related
I have a project folder name utilities.
The list of directory is:
- utilities
- tli
- database
Connection.php
index.php
The Connection.php is PDOConnection.
The code is:
<?php
namespace app\tli\database;
use PDO;
use PDOException;
Class Connection
{
private $server = "mysql:host=localhost;dbname=ytsurumaru_hanwa_coil_v.2";
private $user = "root";
private $pass = "";
private $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
protected $con;
public function openConnection()
{
try {
$this->con = new PDO($this->server, $this->user, $this->pass, $this->options);
return $this->con;
} catch (PDOException $e) {
return "There is some problem in connection: " . $e->getMessage();
}
}
public function closeConnection()
{
$this->con = null;
}
}
UPDATED SOURCE
Now, I need this Connection instance in index.php
<?php
namespace app;
use app\tli\database\Connection;
use PDOException as PDOEx;
require('tli/database/Connection.php');
try {
$connection = new Connection(); // not found
$connection->openConnection();
} catch (PDOEx $e) {
echo $e->getMessage();
}
When I run it,
D:\wamp64\www\utilities\tli>php index.php
Warning: require(tli/database/Connection.php): failed to open stream: No such file or directory in D:\wamp64\www\utilities\tli\index.php on line 8
Fatal error: require(): Failed opening required 'tli/database/Connection.php' (include_path='.;C:\php\pear') in D:\wamp64\www\utilities\tli\index.php on line 8
How to solved this, is my namepace have a problem ?
Isn't this enough to access your database connection?
require 'tli/database/Connection.php';
Then, since you are in a different name space and you are not aliasing, in your 'try catch block' you should instead of:
$connection = new Connection(); // not found
Do something like:
$connection = new \tli\database\Connection();
Make sure to set your paths right.
OR
You can alias to a different name like so:
namespace app;
require 'tli/database/Connection.php';
use tli\database\Connection as MyConnection;
$connection = new MyConnection();
You need to use one of these:
include('tli/database/Connection.php')
include_once('tli/database/Connection.php')
require('tli/database/Connection.php')
require_once('tli/database/Connection.php')
or if you want some more automation use autoloader.
You may want to look at this SO question and all the linked things.
I actually trying to check my connection in connection file tester to check if it is connected to the database but the problem is I got this error
Warning: include(obj\database_connection\SqlHandler.php): failed to open stream: No such file or directory in /home/devhostt/public_html/bcc/gradingsystemmodule/root/root.php on line 16
Warning: include(): Failed opening 'obj\database_connection\SqlHandler.php' for inclusion (include_path='/home/devhostt/public_html/bcc/gradingsystemmodule:.:/opt/alt/php55/usr/share/pear:/opt/alt/php55/usr/share/php') in /home/devhostt/public_html/bcc/gradingsystemmodule/root/root.php on line 16
Fatal error: Class 'obj\database_connection\SqlHandler' not found in /home/devhostt/public_html/bcc/gradingsystemmodule/function/checkconnection.php on line 8
and here is my code to set & get the path (root.php)
<?php
error_reporting( E_ALL );
define("setRealpath", realpath("../"));
const ERROR_EXCEPTION_MESSAGE = "SOMETHING WENT WRONG HERE: ";
try
{
$getPath = array(setRealpath, get_include_path());
if(!set_include_path(implode($getPath, PATH_SEPARATOR)))
{
define("setRealpath", realpath("./"));
$getPath = array(setRealpath, get_include_path());
set_include_path(implode($getPath, PATH_SEPARATOR));
}
function GetClassFile($class)
{
$file = str_replace('/', '\\', $class).".php";
include $file;
}
spl_autoload_register('GetClassFile');
}
catch(Exception $x)
{
die(ERROR_EXCEPTION_MESSAGE.$x->getMessage());
}
?>
here is the file where my connection (SqlHandler.php).
<?php
namespace obj\connection_database;
use \PDO;
class SqlHandler extends Connection
{
const ERROR_EXCEPTION_MESSAGE = "SOMETHING WENT WRONG HERE:";
protected $db = null;
public function __construct()
{
$this->db = $this->getConnection();
}
public function checkConnection()
{
if($this->db)
{
return "CONNECTED";
}
return "NO CONNECTION";
}
}
?>
and lastly here in this file where i'm trying to check the connection(checkconnection.php)
<?php
error_reporting(E_ALL);
include "./root/root.php";
use \obj\database_connection\SqlHandler;
$checkConnection = new SqlHandler;
echo $checkConnection->checkConnection();
?>
now i found my mistake, the only mistake is that i'm using the old version of php, instead of using php 7.1 version , i'm using php 5. so i change it and it works.
Greeting's I'm currently taking my first attempt at developing a login for my website. For this login I'm utilizing PHP and MySQL. My problem is that I keep recieving the error below... I've tried exhausted all possibilities of how to rename "require_once" input to my knowledge, but still know luck.
Warning: require_once(C:\Users\Derek\Fall_2013\PS-WebDesign\includes\constants.php) [function.require-once]: failed to open stream: No such file or directory in D:\Hosting\11311638\html\classes\Mysql.php on line 3
Fatal error: require_once() [function.require]: Failed opening required 'C:\Users\Derek\Fall_2013\PS-WebDesign\includes\constants.php' (include_path='.;C:\php\pear') in D:\Hosting\11311638\html\classes\Mysql.php on line 3
Below is the line of code that is producing the Fatal error, My question is have I not called the file correctly or is there code my feeble mind is missing? The error is on the 3rd line "require_once...."
<?php
require_once 'includes/constants.php';
class Mysql {
private $conn;
function __construct() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die(
'You ***'d up.');
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM users
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}
The path either needs to be an absolute path to the file you want to include or you need to add its location to your include path.
http://php.net/manual/en/function.set-include-path.php
is this include file in the root directory?
You should also remove the blank line above the include line.
Lsatly, the syntax for die should read die(mysqli_error() 'You ***'d up.');
Hi i am trying to import the configuration variables from a config file that i had created
Config.php has
class Config
{
public $SERVERNAME = '*******';
public $SUSERNAME = '*********';
public $SPASSWORD = '*********';
public $DBNAME2 = '********';
}
for importing this variable in another file i tried
include('./Config.php') or die("error");
$cnfig = new Config();
But when i try to use $cnfig->DBNAME2 its not working
Please help to sole this problem.
Thank you.
EDIT
It was working in localsystem , when i tied to upload through web hosting it was not working.
Try below one
include ('./Config.php');
$cnfig = new Config();
echo "<pre>";print_r($cnfig);
Edit:
I am getting below output so it should be working for you too.
Config Object
(
[SERVERNAME] => *******
[SUSERNAME] => *********
[SPASSWORD] => *********
[DBNAME2] => ********
)
Edit:2
You can also do like this
(include('./Config.php')) or die("error");
$cnfig = new Config();
echo "<pre>";print_r($cnfig);
http://www.php.net/manual/en/function.include.php#39043
or has higher priority than include()
Error Ouput:
When only use include('./Config.php') or die("error"); it's giving below error.
Warning: include(1) [function.include]: failed to open stream: No such file or directory in D:\path_to_file\myConfig.php on line 2
Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.;D:\ZendServer\share\ZendFramework\library') in D:\path_to_file\myConfig.php on line 2
Fatal error: Class 'Config' not found in D:\path_to_file\myConfig.php on line 6
I have a 3 file:
class.database.php
config.php
test.php
In class.database.php i using:
<?php
class Database {
private $db_connect;
function __construct($config) {
$this->connect($config);
}
function connect($config) {
$this->db_connect = #mysql_connect($config['hostname'], $config['dbuser'], $config['dbpass']) or die("Can't connect to mysql server");
#mysql_select_db($config['dbname'], $this->db_connect) or die("Can't select database mysql server");
$this->query('set names utf8');
}
function disconnect() {
mysql_close($this->db_connect);
}
}
?>
in config.php i using:
<?php
include_once 'class.database.php';
$config['hostname'] = 'localhost';
$config['dbuser'] = 'root';
$config['dbpass'] = '';
$config['dbname'] = 'test';
$db = new Database($config);
?>
And test.php i using:
<?php
include 'config.php';
include 'class.database.php';
when to run test.php in wampserver is host alert error is Fatal error: Cannot redeclare class Database in class.database.php on line 2
How to fix it ?
You're includeing the class.database.php file twice, which gives this error. Use include_once for it always, not include.