Thursday, February 25, 2010

Hook me up

I'm all done with classes for the week, got some time before the evening activities, sitting in the 34th & Walnut Starbucks... goals for the session are: to connect via PHP with the test database I set up on my server space - at the least to pull info from it and spit it out onto the page, if not to insert new records to the one table - and to get a few calls to the Facebook API working via the Facebook PHP client library.

First I'm checking to make sure PHP works in the first place.  I made my test.html page into test.php and threw in a call to phpinfo().  It works, so it looks like I'm good to go.  Now to figure out database access.  I created a new database user and gave the user access to the app database (which currently only has the one test table).  I'm not sure yet exactly what functionality the app will need to have in accessing the database, so I somewhat arbitrarily gave the following permissions for now: select, insert, update, delete, drop, and lock tables.

In search of a refresher on integrating a MySQL database, I came upon this more general tutorial on building various parts of a Facebook app.  (For now I am only referencing the MySQL example section.)  For organization's sake, I created a file called db_connect.php just to encapsulate the connection code.  Following the example, I inputted "localhost" as the host (following this Support Center answer) along with the user and password I just created.  I also looked up the PHP documentation for the mysql_connect function and based on the example there I added a test for whether the connection attempt was successful.  Since no error message appeared, I assume the connection worked.  The last step is selecting the particular database to use.  I added a call to mysql_select_db and a test for success.  This worked as well.


Redux: So I think I might be documenting my every action a little too carefully, because this took more time than it should for only a bit of code.  I didn't make it to actually pulling from or inserting into the database, and nowhere close to trying out Facebook API calls.  But I figured out the right parameters to connect and it worked, so I did make it that far.

No comments:

Post a Comment