'file_get_contents() not working with require_once() in php
I am trying to initialize a database once my index.php file gets open in browser for the first time.
I had first deploy.php that had an html code to specifically on submit it sends $_POST information to initialize. "it was working just fine".
Then I thought of better solution require_once(deploy.php) at the top of my index.php file, and now my file_get_contents() not returning the query into the variables and receiving an error that
General error: trying to execute an empty query
My code in deploy.php:
try {
$USER_TABLE_QUERY = file_get_contents("./usertable.sql");
$conn = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("$_USER_TABLE_QUERY");
} catch(PDOException $error) {
echo $USER_TABLE_QUERY . "<br>" . $error->getMessage();
}
contents of usertable.sql :
CREATE TABLE IF NOT EXISTS `user` (
user_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(150) NOT NULL,
fullname VARCHAR(150) NOT NULL,
email VARCHAR(150) NOT NULL,
`password` VARCHAR(150) NOT NULL,
`date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
EDIT NOTE:
I have even created a very simply php code:
<?php
$_sql = file_get_contents("hello.txt");
echo "this is sql:".$sql;
?>
and the content of hello.txt:
hello world
I have moved all the files to be in the same directory with each other but yet file_get_contents() not returning anything
is it somehow require_once() not executing file_get_contents() function?
Solution 1:[1]
I got it fixed by:
Adding all the code that was inside deploy.php inside index.php and now file_get_contents() working just fine, even though I still don't understand why deploy.php wasn't able to use file_get_contents() inside of it. And I hope if someone willing to still provide more clear answer about this! Thanks :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alsakka |
