Reputation: 4633
I made a .bat script, but when I run it, it doesn't detect the included file in my php script.
.bat
"C:\xampp\php\php.exe" -f "D:/Projects/Web projects/done/sticky/test.php"
test.php
<?php
include 'db.php';
try {
//stm
$STH = $DBH->prepare( "INSERT INTO users (username, password, email) values (:user, :pwd, :email)" );
//bind
$data = array( 'user' => 'a', 'pwd' =>'b', 'email' => 'c' );
//exec
$STH->execute( $data );
}
catch( PDOException $e ) {
file_put_contents( 'PDOErrors.txt', $e->getMessage(), FILE_APPEND );
die( "db error" );
}
?>
db.php contains my db info, but the script doesn't detect it. Do note that if I run it normally in my browser, it does work.
Upvotes: 0
Views: 41
Reputation: 524
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__) . DS);
require_once(ROOT . 'db.php');
Upvotes: 1
Reputation: 318518
Includes in PHP are relative to the current working directory, not the script's location. However, when accessing a PHP file via a webserver the CWD is usually the location of the script.
In your case you either need to change the directory before running the script or change it in your script using chdir(__DIR__);
Upvotes: 2