Sunday, August 30, 2015

[IT] Connecting PHP with Microsoft Access (.mdb or .accdb)




WHAT I USED:
1. XAMPP 2.5 (PHP 5.3.8)
2. Microsoft Access 2007


STEPS:
1. Prepare the database on Microsoft Access. Here, I used 2002-2003 format (.mdb).

(picture 1: a first  Microsoft Access 2007 interface)


(picture 2: preparing a new database named phptoacc.mdb)

(picture 3: preparing new table named testdb with its fields, testid and testname)


(picture 4: inserting two new records)

2. Save the database where the PHP can easily reach it. Here, I saved it on "access" folder inside "htdocs" folder of "xampp" folder.

(picture 5: where I stored my database)

3. Type the PHP script for trying to connect to the database.


<?php

$db = $_SERVER["DOCUMENT_ROOT"] ."/access/phptoacc.mdb";
if (!file_exists($db))
{
       die("No database file.");
}

$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
$sql = "select * from testdb";
$rs = $dbNew->query($sql);

while($result = $rs->fetch())
{
     echo $result[0].": ".$result[1]."<br />";
}


?>



I will explain each part of the script:

a. SELECTING DATABASE

$db = $_SERVER["DOCUMENT_ROOT"] ."/access/phptoacc.mdb";
if (!file_exists($db))
{
        die("No database file.");
}

The above script is used to validate whether the phptoacc,mdb exists on "access" folder.


b. CONNECTING TO THE DATABASE

$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");

The above script uses the PDO driver to connect to Microsoft Access.



NOTE:
If the error message show "PDOException Could not find driver", you must check whether the PDO ODBC driver is installed or not. You can check it by executing:
<?php  phpinfo() ?>
 The output should show information like:

PDO

PDO supportenabled
PDO driversmssql, mysql, odbc, sqlite, sqlite2

pdo_mysql

PDO Driver for MySQLenabled
Client API versionmysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $

PDO_ODBC

PDO Driver for ODBC (Win32)enabled
ODBC Connection PoolingEnabled, strict matching


If an entry for PDO ODBC is not present, you will need to ensure your installation includes the PDO extension and ODBC drivers. To do so on Windows, uncomment the line extension=php_pdo_odbc.dll in php.ini, restart Apache (or XAMPP), and then try to connect to the database again. (source: http://www.sitepoint.com/using-an-access-database-with-php/)

c. THE SQL TO SHOW THE RECORDS

$sql = "select * from testdb";
$rs = $dbNew->query($sql);

while($result = $rs->fetch())
{
     echo $result[0].": ".$result[1]."<br />";
}

The above script is used to show the records stored on testdb table.



5. Execute the script.

(picture 6: the result on Google Chrome)

Monday, March 2, 2015

[LIFE] What Happened in 2014?



I know, I know, it's soooo late to post this kind of post. I wrote this post on December 2014 and it stayed on the draft because I haven't got the chance (or mood) to complete it. When I opened my blog today and saw my draft, I just got the power to finish it all of sudden, so I took the chance before my beloved mood went away.

On this post, I would like to review some life-changing events that came in my life in 2014 and I would like to share the lesson that I learnt from it. I hope you guys see it as a sharing moment, not pompous review.


1. THESIS AND THE DEFENSE
If you guys read my previous post about my thesis experience, you will see that for me, thesis is such a tiring things to experience yet when you survive, you will see the beauty of graduation. For college students facing thesis, sleepless nights must be their best friends.
Sometimes, we faced a dead-end, yet we just felt that it was worth the fight. My thesis team consisted of three people including me. They were the ones that fight with me all along. Sometimes we quarreled over a small tiny things! Hahaha... However, I would not make it without them.

LESSONS LEARNT
a. Whatever happens, the members of the team sticks together.
Yes, we faced many problems and helpless moment. At the last day of submitting the topic, we met the zero! Nothing! Because the last topic we proposed was not acceptable and so far of our imagination. Yet, we still stuck together. I remember the night like yesterday. We gathered on my friend's room and searched for an available topic. Silent. Just sigh breath. We made it! We were fearless and faithful.

When my father passed away, they were the ones that I could rely on although the deadline was coming like a chasing dog. I thought that we're bond by the same goal. When a team comes with the same bonding goal, they won't break the bond and keep fighting together.

b. Preparation matters.
I thought we could do better if we had more preparation. The schedule for our thesis defense came suddenly and we're not ready at all. We had some revision on our paper and we had not finished it yet. A day before the defense, we just finished the revision and completely had time to study the material.
Some of my friends with better preparation didn't face the problems which we encountered. Yes, learn it from us, prepare yourself with the best things long before you face it. The sharp knife will cut the tree easier.


2. TEARS IN HEAVEN
I lost my father right before he could see me graduate. In the middle of my last exam, in the middle of my thesis deadline, right after the happiness of Chinese New Year, I lost one of my greatest hero. Well, I never thought that I would lose my father at such a young age. As a child, I would always think that my parents would be with me for endless period of time. Do you feel the same? I saw the death of my friend's loved ones before and I could not imagine it happen to me. It's like a hole on your heart that will never heal. You lost the piece of your heart forever.
I remember the last conversation I had with my father. It was the Chinese New Year morning, when I called to say the new year wish to my father. He cried without a response to my wish. He could not say anything at all. I could not imagine that was the last time I heard my father's voice and breath.




LESSONS LEARNT
You will see that how lame your excuse to make them happy after you're successful. When you're growing, they keep getting older. No way, we must not wait. We cannot make them happy, all we can do just sit there with them and talk anything with them. Spend the time with them. If you could see my life, you would understand how regretful I'm. They don't need your money, they just need to see your smile and share the life with them.

3. THEY ARE GROWING OLDER AND WEAKER
My father was diagnosed with stroke in the middle of 2014. It came so suddenly and I would never think my father could suffer and have a poor health. This opened my eyes that my parents was growing older, older, and weaker. They won't be always young and waiting for you to come home.

LESSONS LEARNT
Like point #2, they need you now.


4. GRADUATE AND WELCOME TO THE REAL WORLD
I graduated and looked for a job right after. I never imagined that looking for a job was such a tiring experience. I applied for various jobs. I got the chance to be interviewed for some of them and some of them was silent-till-the-end. Sad, exhausted, and inspired. Take a look of my experience posted on this post.


LESSONS LEARNT
Never underestimate the power of CV and interview preparation. Review your CV periodically and never use same CV for different job area application. See my tips for job interview at this post (sorry, it was written in Indonesian and I haven't got the mood to translate that).

Powered by Blogger.