Home arrow MySQL arrow PHP and Databases for the Lazy Sod
MYSQL

PHP and Databases for the Lazy Sod


In this article Justin explains his ezSQL utility, which reduces code and development time for database operations in PHP. This is great for the lazy sod...and everyone else.

Author Info:
By: Justin Vincent
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
January 19, 2003
TABLE OF CONTENTS:
  1. · PHP and Databases for the Lazy Sod
  2. · Atomic Operations
  3. · Use PHP Functions not DB Functions!
  4. · Abstraction
  5. · Getting Even Lazier
  6. · Conclusion

print this article
SEARCH DEVARTICLES

PHP and Databases for the Lazy Sod
(Page 1 of 6 )

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.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials