error in getting the fetching path - php

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.

Related

How to access database connection and base url in child folder?

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.

PHP - Create PDO Connection using namespace

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.

How to have more than one include on the same page in PHP

The title says it all, I've been trying to get this to work but it isn't working for me. It is just displaying the first include.
<?php include 'config.php'; include 'https://theurl/that_the_header_is_on/header.php'; include '$header_url'; ?>
EDIT: The error is here:
[11-Dec-2016 03:26:13 Europe/Moscow] PHP Warning: include(): http://
wrapper is disabled in the server configuration by allow_url_include=0
in /home/myurlf/public_html/SimplePages/index.php on line 4
[11-Dec-2016 03:26:13 Europe/Moscow] PHP Warning:
include(myurl.fluctis.com/SimplePages/Default/1.0/header.php‌​):
failed to open stream: no suitable wrapper could be found in
/home/myurlf/public_html/SimplePages/index.php on line 4 [11-Dec-2016
03:26:13 Europe/Moscow] PHP Warning: include(): Failed opening
This should make code run. But...
<?php
include('config.php');
include('https://theurl/that_the_header_is_on/header.php');
include($header_url);
?>
Are you trying to do this? .. your post is a little confusing
<?php
include('config.php');
$header_url = 'Includes/header.php';
include($header_url);
?>
Try this. This is what I currently using.
/**
* Load all the class under RTlib\RTphp
*
* #return boolean False if error
*/
function RTphpLoad() {
$lib = array();
$lib[] = '/src/RTmysqli.php';
$lib[] = '/src/RTpassword.php';
$lib[] = '/src/RTslugify.php';
$lib[] = '/src/RTutil.php';
try {
foreach ($lib as $class) {
require_once dirname(__FILE__) . $class;
}
return true;
}
catch (Exception $ex) {
trigger_error('RTphpLoad() Fail to load. ' . $ex->getMessage());
return false;
}
}
In another php file
require_once FILEROOT . 'RTlib/RTphp/load.php';
RTphpLoad();

PHP fopen succeeding for one script but not another

My following method uses an array of data $FDFData to create an FDF file:
public function writeFDFFile($FDFData) {
$fdf_file = time().'.fdf';
$fdf = $this->createFDF(PDF_FORM, $FDFData);
// write the file out
if($fp=fopen($fdf_file,'w')) {
fwrite($fp,$fdf,strlen($fdf));
} else {
throw new Exception('Unable to create file: '.$fdf_file);
}
fclose($fp);
return $fdf_file;
}
One of my scripts runs absolutely fine:
require_once('PDFPrinter.php');
try {
$printer = new PDFPrinter();
$FDFData = $printer->assembleFDFData(9);
$fdf_file = $printer->writeFDFFile($FDFData);
$printer->downloadFile($fdf_file);
}catch(Exception $e) {
echo $e->getMessage();
}
but I can't get my test code to run because I get a:
Unexpected PHP error [fopen(1315558352.fdf) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied]
error thrown:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once(dirname(__FILE__) . '/../PDFPrinter.php');
class PDFPrinterTest extends UnitTestCase {
private $printer;
private $FDFData;
function setUp() {
$this->printer = new PDFPrinter();
$this->FDFData = $this->printer->assembleFDFData(5);
}
function testException() {
try {
$this->printer->writeFDFFile($this->FDFData);
$this-assertTrue(true);
} catch(Exception $e) {
$this->assertTrue(false);
}
}
}
Both scripts are run in directories with the correct permissions. I am running my test scripts through the browser as well, so it's not that I have a different environment.
I'm stuck as to how to proceed to find the issue really.
My directory structure:
HOME - PDFPrinter.php
|
-----tests - PDFPrinterTest.php
|
------simpletest - autorun.php
Any suggestions as to how I could find the issue?
Many thanks
Update
I have tried changing my test class so that the only test function in there is:
function testWrite() {
try {
$name = "testing.txt";
if($fp=fopen($name, 'w')) {
fwrite($fp, "Blah");
} else {
throw new Exception("Nope.");
}
$this->pass("All good");
} catch(Exception $e) {
$this->fail("Not good");
}
}
but the exception is still thrown with the warning.
Yet a very simple script run from the same directory works fine:
$name = "Test.txt";
if($fp=fopen($name, 'w')) {
fwrite($fp, "Working");
} else {
throw new Exception("Nope.");
}
fclose($fp);
that will actually create and write to the file.
Finally found the solution which was that the file name needed to be the full absolute address in order for it to work in both scripts for some reason. This was suggested in one of the answers for this SO question which I quote below:
Use fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');
so for my code, I have used:
if($fp=fopen($_SERVER['DOCUMENT_ROOT'].$name, 'w')) {
fwrite($fp, "Working");
}
In your test
fwrite("Blah");
should be
fwrite($fp, "Blah");
I'm not sure what the problem in the original code is though.
failed to open stream: Permission denied
There is one important programmer's skill every developer ought to master.
Here it is:
To trust your eyes
If your PHP telling you that permission is denied - so it is. Just doble-check it. It is not a big deal yet noone can do it for you.

Pear DB class not found

I have create a class called Database.php for interacting with MySql database using Pear Db class.
Database.php
<?php
require_once('DB.php');
require_once('cException.php');
class DataBase
{
private $dsn = 'mysql://root:xxxxxx#localhost/avatar';
private $conn;
//Constructor
function __construct()
{
global $conn;
$this->conn = DB::connect($dsn);
if(DB::isError($conn))
{
throw new DatabaseConnectionException();
}
}
//destructor
function __destruct()
{
$this->conn->disconnect();
}
public function select($query)
{
$conn->setFetchMode(DB_FETCHMODE_ASSOC);
$result = & $conn->query($query);
if(DB::isError($result))
{
return new SelectCommandException($result->getMessage());
}
return $result;
}
static public function instance()
{
static $objDB;
if(! isset($objDB))
{
$objDB = new DataBase();
}
return $objDB;
}
?>
And I am calling this class from a sample file test.php
test.php
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
require_once 'Database.php';
try
{
$db = DataBase::instance();
}
catch (DatabaseConnectionException $ex1)
{
echo $ex1->toString();
}
try
{
$sql = "Select * from register";
$result = $db->select($sql);
var_dump($result);
}
catch (SelectCommandException $ex2)
{
echo $ex2->toString();
}
?>
When I run test.php I get the following error
Warning:
require_once(/usr/share/pear/DB.php):
failed to open stream: No such file or
directory in
/var/www/Avatar/Database.php on line 2
Fatal error: require_once(): Failed
opening required
'/usr/share/pear/DB.php'
(include_path='.:/usr/share/php:/usr/share/pear')
in /var/www/Avatar/Database.php on
line 2
I don't know why I am getting this error. In phpinfo() it shows include_path .:/usr/share/php:/usr/share/pear .:/usr/share/php:/usr/share/pear
I am using php5 and I even tried installing php-pear package, still I get the same error.
I don't understand what's wrong here. Can someone please point me in a right direction.
Note : I have not installed php5 using sudo apt-get install php5. I have downloaded php5 packages using Keryx application.
Looks you did not install DB package, try in command prompt, do
pear list
If no package of DB is installed, you can installed it with
pear install DB
example from documentation

Categories