I discovered that phpMyAdmin's PHP export does not include the <?php
tag at the beginning of the file. That is, the content of the files look something like this:
/* `production`.`people` */
$people = array(
array('id' => '999','name_first' => ...
Instead of this:
<?php
/* `production`.`people` */
$people = array(
array('id' => '999','name_first' => ...
This essentially breaks include
and require
because the code will be interpreted as HTML rather than PHP.
How should I load and run the code to get the array of data? I know that eval
Is Bad but I'm not familiar with other ways to accomplish this.
For the record, this will be run only in development and testing environments, never in production.
You can just manually add the <?php
tag at the beginning of the file content before including or requiring it. This avoids using eval
.
This might help:
<?php
$filePath = 'path/to/file.php';
$fileContent = file_get_contents($filePath);
if (strpos($fileContent, '<?php') !== 0) {
$fileContent = "<?php\n" . $fileContent;
}
$tempFilePath = 'path/to/temp_file.php';
file_put_contents($tempFilePath, $fileContent);
include $tempFilePath;
unlink($tempFilePath);
print_r($people);
You can make it into a function.