Free Newsletters:
DatabaseDaily  
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner

internet.commerce
Be a Commerce Partner


















eBay Sees Strong Quarter on PayPal Growth

Mozilla Patches 14 Firefox Security Flaws

Juniper Sees Lift From Carriers, Enterprise IT

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2









Market Data Administrator - Financial (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

PHP

October 22, 2002

PHP and Working with Databases (for the Lazy Sod)

Database Abstraction and Atomic Operations and Classes Explored
By Justin Vincent


The Lazy Sod

Maybe it's just me, but after building database driven websites in PHP for the past six years I am starting to get more than a little tired of repeating myself. What I mean to say is... how many times, on how many different projects, and with how many different databases do I have to write something along the lines of:

<?php

	mysql_connect("localhost", "mysql_user", "mysql_password") 
		or
	die("could not connect");

	mysql_select_db("mydb");
	
	$result = mysql_query("SELECT id, name FROM mytable");
	
	while ($row = mysql_fetch_array($result)) 
	{
		printf ("ID: %s  Name: %s", $row[0], $row["name"]);
	}

	mysql_free_result($result);

?>
Is there really any need to make my fingers type this kind of gunk so often? Surely there must be an easier way of working with databases than this? And if so, where do I find that kind of information?

You might say 'abstraction' and bring up names like PEAR and ADOdb, and you might be right (if the only thing important to you is being able to use the same code with different databases). The problem is you still have to type out lots of stuff whenever you want to deal with a database. For example, here is the required code using ADOdb:

<?

	include('adodb.inc.php');
	
	$conn = &ADONewConnection('access');
	
	$conn->PConnect('northwind');
	
	$recordSet = &$conn->Execute('select * from products');
	
	if (!$recordSet)
	{
		print $conn->ErrorMsg();
	}
	else
	{
		while (!$recordSet->EOF) 
		{
			print $recordSet->fields[0].' '.$recordSet->fields[1].'
'; $recordSet->MoveNext(); } } $recordSet->Close(); $conn->Close(); ?>
Nothing wrong with that, you say. I agree. You can use the same code on lots of different databases, you say. Fair enough, but I want to be even lazier than that. Ah, you say.


Atomic Operations

After extensively scouring the net for examples of how to be a really 'lazy sod' with databases and PHP, I found absolutely nothing that could help me. So like the old adage goes, "it's hard work being lazy", I set about putting some real mental effort into resolving this problem. What I realized was that I should first break down the problem into a list of atomic operations and requirements (for those not in the know, an atomic operation is an operation that does one thing only - and does it well).

After all, when you stand back and look at it, what does working with databases using PHP really mean? As far as I can see, the majority of the time you only need four or five atomic operations to do 'most things' that you need to do:

1) Perform a non-result query such as Insert/Update/Commit
2) Get a single variable from the database
3) Get a single row/column from the database
4) Get a list of results from the database

In fact, when I think about it, all the commercial and non-commercial PHP projects that I have ever worked on have never needed any other operation. I'm not even sure you can do any other operation with a database... Before you scream blue murder, I am not talking about SQL queries here, I am talking about the functions that wrap up the SQL queries. Because no matter how complex the SQL query you write, only one set of results will ever be returned -- and as we'll see, that's a good thing.


Page 2: Query Result Sets




Go to page: 1  2  3  4  5  6  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

PHP Archives








Latest Forum Threads
PHP Forum
Topic By Replies Updated