Pretty new to php and having issues with loading values from an external config file. I'm using it for my database connection and sometimes it will return the values, but sometimes it returns null. I've been fighting with this for hours and can't seem to find any info on this. The external file is formatted like this
[database]
db_name = "atmc_timesheet"
db_user = "username"
db_pass = "password"
I'm accessing it like this
function connect_db()
{
$ini = parse_ini_file("../../../atmc_timesheet_config.ini", TRUE);
$dbhost = 'localhost';
$db_name = $ini['database']['db_name'];
$user_name = $ini['database']['db_user'];
$pass = $ini['database']['db_pass'];
$dsn = "mysql:dbname=$db_name;host=$dbhost";
try {
$pdo = new PDO($dsn, $user_name, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
catch (Exception $e) {
echo "Error : ".$e->getMessage();
}
}
It works for my login script, but the next query I run returns null for the data. Both functions accessing this are in the same php file, so it's not an error regarding a bad path. Any thoughts?
The working script is
function get_login($username)
{
$pdo = connect_db();
$stmt = $pdo->prepare('SELECT * from tblemployees where username = :username');
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();
$stmt = null;
$pdo = null;
return $user;
}
The non working one is
function get_events_for_calendar($userid)
{
//Gets all userid rows
$pdo = connect_db();
$sql = "Select * from tblevents where (employeeId = :uid) order by eventId desc;";
$stmt = $pdo->prepare($sql);
$stmt->bindvalue(':uid', $userid);
$result = $stmt->execute();
$temp_array = array();
$event_array = array();
//fetches records and puts in array
while ($record = $result->fetch()){
$temp_array[] = array('id' => $record['eventId'], 'empolyeeid' => $record['employeeId'], 'rh' => $record['rh'], 'oh' => $record['oh'],
'inventory' => $record['inventory'], 'empapproved' => $record['empapproved'], 'mgrapproved' => $record['mgrapproved'],
'acctapproved' => $record['acctapproved'],);
$var_dump($temp_array);
}
echo json_encode($event_array); }
The one that is not working gets null values form the ini file on username and password, but gets info on the host variable.
It's not finding the file on the second query even though both queries are in the same php file. Is it possibly not closing the ini file and that is the reason for the error.I can't find any info about closing after a parse_ini. Any thoughts why it wouldn't find it.