GetOne and GetCol

ADODB has a few other features that are quite convenient — GetOne() and GetCol()

GetOne() extracts a single value from a database query into a PHP variable. For example:

$userid = $db->GetOne("SELECT userid FROM users WHERE users.username='$username'");

The code above would place the userid in the PHP variable $userid, based on the lookup by username.

GetCol() returns an array of values found for a single column in a database table, as follows:

$active_users = $db->GetCol("SELECT userid FROM users WHERE active='$active'");

The above code places an array of userids in the PHP variable $active_users.

These are just a couple of ways that ADODB can make your life a lot easier when it comes to database queries.

On top of ease-of-use, ADODB allows portability, since it supports many databases. Even if you are not planning to use another database anytime soon, ADODB still makes programming database queries a lot more simple!

ADODB Selects

Before doing anything else, you need to connect to your database. This is pretty simple with ADODB:

$db = &ADONewConnection('mysql');
$db->SetFetchMode(ADODB_FETCH_ASSOC);
$db->PConnect($dbserver, $dbuser, $dbpass, $dbdefault);

The first line connects you to the MySQL database (or other database of your choosing). The second line is not necessary, but is my preference — it makes ADODB return result sets as associated arrays with the keys being the field name from the table. The third line connects to the database (PConnect for persistent connections).

ADODB has most of the features that you would expect, such as selecting a single row:

$this_user = $db->GetRow("SELECT firstname, lastname FROM users WHERE userid='1'");

Or selecting multiple rows:

$texas_users = $db->GetAll("SELECT firstname, lastname FROM users WHERE state='TX'");

It conveniently returns the above entries in an array. GetRow() returns the array just for that record and the GetAll() returns an array of arrays for all retrieved rows.

One of my favorite commands in ADODB is GetAssoc(). This returns an array that has the key as the first field in your select statement and the array value as the second field. For example:

$rs = $db->Execute("SELECT userid, name FROM users");
$users = $rs->GetAssoc();

The resulting array $users is an array of all users with the userid as the key and name as the value. This is perfect for use with HTML dropdown boxes as well as many other uses!

Intro to ADODB

Whether you are new to databases and PHP or have used MySQL/PostGreSQL or some other database for quite some time, there is a tool that can assist you in you development process: ADODB

ADODB is a “database abstraction layer”. Basically this means that you can create your database code once and then use it on whatever database suits your needs at the time. That is, you can start out using MySQL and upgrade to Oracle later on without making any code changes!

That might be a little simplified, but that’s the idea. But even if you are going to stick with a single database, such as MySQL, it’s still work a look.

Over the next few days, I’ll cover a few of the ways that ADODB can make your life as a PHP/Database programmer a lot easier!

Using header to avoid form reload issues

A common problem with web forms is that users can hit reload in their browser and resubmit the form. This can cause problems ranging from simple inconveniences, such as duplicate discussion board posting, to major issues, such as duplicate orders being places through a web store.

A simple way to prevent a user from reloading form values is to redirect the user to a new page after the form is submitted. To do this you can use PHP’s header command.

Here’s a simple redirect using PHP:

header("Location: http://www.yourwebsite.com/thanks.php");
exit;

These two lines of code simply redirects the user to your “thanks.php” page. The exit command ensures that no further code will be processed on that page.

After the user is redirected to the new page (which would normally show some sort of thank you message, order confirmation, etc.) they can hit reload as many times as they like and the form will not be resubmitted.

Note that this does not solve the problem of the user going back in their history and resubmitting a form — for that you should use other error checking, such as checking whether a user has already submitted an identical order, etc. — but it’s a useful tool to avoid problems with users hitting reload after submitting a form.