Easy MySQL in PHP

Scott pointed to an article on PHP and Working with Databases (for the Lazy Sod), so although I’m very happy with the class I’m using for MySQL access now, I took a look.

The author seems to be saying reasonable things: abstraction in the PEAR/ADOdb sense (abstracting away which database engine, while still requiring tons of low-level detail) isn’t enough for a lazy weekend programmer like me; you only need to do four things with your db (a non-result query, get a single variable, get a single row/column, get a list of results); sometimes you want your results as an associative array, sometimes a numerically indexed array is more convenient, most of the time in PHP object syntax is best, so you can access things by the column names while still embedding your variables in a quoted string (I hate having to say “and this is ” . $var[‘this’] . “and that…”). In fact, I was a little surprised that the author didn’t come across my new pet database class before he went to the trouble of writing his own, since he seemed to be describing it exactly. Teach me not to pay attention to the developer’s name, since on page three when he finally mentions his class by name, it turns out to be ezSQL, my new favorite. If you use MySQL, Oracle8, or InterBase/FireBird databases from PHP, and you’re a lazy sod like Justin and I who would prefer saying


<?php
include_once "ez_sql.php";
$users = $db->get_results("SELECT * FROM users");
foreach ( $users as $user )
{
echo "ID: $user->id Name: $user->name";
}
?>

rather than


<?php
mysql_connect("localhost", "mysql_user", "mysql_password")
or
die("could not connect");
mysql_select_db("mydb");
$result = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_array($result))
{
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>

then it’s worth a look.

6 Comments

Comment by Lach #
2002-11-09 03:59:09

Or you could be a lazy and inefficient sod like me and just auto prepend the database connection into all your php pages so you don’t have to bother about connecting ever. No need for an include statement even. :)

Of course my next big project is going to be rewriting the way my site works to use xml-like tags instead of having to dip into php syntax at all, and then processing it with an auto appended file. The above example would look something like this.

<php:dbquery sql="SELECT * FROM users">
ID: <php:var source="SQL" name="id"/> Name: <php:var source="SQL" name="name"/>
</php:dbquery>

And then the script would query the databse, loop through all the entries automatically, print the values out and then close the db connection and free up the variables the SQL created. The lazier, the better!

 
Comment by Shannon #
2002-11-09 22:26:49

Something’s different.

What is it?

 
Comment by Phil Ringnalda #
2002-11-09 22:38:57

Got me. Can I have a hint?

 
Comment by Shannon #
2002-11-10 20:55:35

Sure, now that I’ve figured out what it is. You sidebar doesn’t break at 800×600 anymore. It popped into my head today at work, actually, which is pretty creepy, when you think about it.

”Would you like ketchup with your… oh, that’s it!”

I browse with my favorites open usually, and your sidebar always gets squished to the bottom. But not yesterday. Maybe it’s just that you usually have code in your entries or something.

Anyway, that’s my completely pointless observation for the day.

 
Comment by Phil Ringnalda #
2002-11-10 21:25:13

Ah, that. When I post code that would change meaning or break if it has extra line breaks inserted by your browser, I put it in a <pre> section, which tends to break the display. When I remember that (or when you remind me), I move it to an extended entry, since there’s nothing to break on the individual entry page.

And you do know that now I’ll be picturing you browsing with your ”favorites open”, don’t you?

 
Comment by Shannon #
2002-11-11 16:12:08

Honey, if that’s what gets you through the day, then have at it.

You do know that now I’ll be picturing you picturing me browsing with my ”favorites opne,” don’t you?

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <del datetime="" cite=""> <dd> <dl> <dt> <em> <i> <ins datetime="" cite=""> <kbd> <li> <ol> <p> <pre> <q cite=""> <samp> <strong> <sub> <sup> <ul> in your comment.