Geeks On PHP Episode 12

In this episode Stephen and I talk about database connections, retrieving results and making use of those results. Both MySQL and PostgreSQL are discussed in this episode so whichever open source database you are using we got you covered.

Here is a link to Stephen’s database class for MySQL. He discusses it’s usefulness and some of it’s functions in the show.

Here is some sample code for getting connected to and pulling out results from a PostgreSQL database. The database include file should be separate from your application code so that you can easily change connection parameters in one location without touching your application.

/* Example of PostgreSQL database include file 
	named */

$host = "hostname";
$port = "port";
$dbname = "database";
$dbuser = "username";
$dbpass = "dbpass";

$hdb = pg_connect("host=$host port=$port dbname=$dbname 
	user=$dbuser password=$dbpass");

/* How to include and open connection in script */

/* How to query a PostgreSQL database */
$res = pg_query($hdb, "SELECT name, email FROM table");

/* Close database connection */

/* Create associative array */
while($ares = pg_fetch_assoc($res))
	// do stuff

/* Create object */
while($ores = pg_fetch_object($res))
	// do stuff

Check both Stephen and Myself out on Twitter and also subscribe to us on iTunes!

Enjoy the show and as always, stay geeky!

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

- Nicholas

Tags: , , , , , , , , , ,

2 Responses to “Geeks On PHP Episode 12”

  1. [...] This post was mentioned on Twitter by Stephen Johnson, ForTheLoveofGeeks. ForTheLoveofGeeks said: new blog post: Geeks On PHP Episode 12: In this episode Stephen and I talk about database connections, retrieving … [...]

  2. Chris M says:

    The link to the class doesn’t work :-(

Leave a Reply