-1

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.

Mike
  • 125
  • 1
  • 1
  • 6
  • Hmm, well what is your working login script and what is the "next query" that returns null? Cant really tell from what you have currently provided. – TimBrownlaw Dec 26 '16 at 04:39
  • It looks like you are trying to make a new PDO connection before every query, which is going to be anice issue. That would be my first guess as to what is causing it. You should make your database connection variable singleton – kunruh Dec 26 '16 at 05:45
  • Like I said, I'm new to php, but I'll look into that. – Mike Dec 26 '16 at 05:47

1 Answers1

0
  • Your current problem is that the path you are using is relative to the current script and therefore not always points to the same place.
  • Your main problem is wrong error reporting. Once properly set, it would tell you everything of your errors, including wrong path you are using.
  • Your additional problem is that connect_db connects every time when called.
  • and another problem is that you don't set the connection charset which is very important.

Below you may find the edited version. It connects only once and it is using an absolute path to your script. You ave no edit the path too. the proper charset is also set The information on error reporting you will find in the linked question.
You may find more info on PDO in my PDO tutorial

function connect_db()
{
    static $pdo;
    if (!$pdo)
    {
        $ini = parse_ini_file($_SERVER['DOCUMENT_ROOT']."path/from/root/atmc_timesheet_config.ini", TRUE);

        $dbhost = 'localhost';
        $dbcharset = 'utf8';
        $db_name = $ini['database']['db_name'];
        $user_name = $ini['database']['db_user'];
        $pass = $ini['database']['db_pass'];

        $dsn = "mysql:dbname=$db_name;host=$dbhost;charset=$dbcharest";
        $pdo = new PDO($dsn, $user_name, $pass);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    return $pdo;
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • This answer worked for me. Your page about the only proper pdo tutorial was actually the first I read when I started working on this project. It's just new to me coming from a .NET environment as php seems to require a new way of thinking when it comes to databases. I appreciate all the help in setting me on the right path. – Mike Dec 26 '16 at 17:18