Hi,

I'm trying to separate my database query code from my application logic.
I want to do this by creating a small application-level library with
functions that INSERT, DELETE and UPDATE specific information in a mysql
database. So I created a file called  news_mysql.lib.php and put it in a
/library directory. It has functions that look something like this:

function get_news_item($id)
{
        // boundary and error checking, strip out potentially nasty SQL
stuff
        // build the SQL statement
        $result = $mysql_query($sql);
        return mysql_fetch_array($result);
}

// and so on...

My news.php file looks something like:

require_once('path/to/library/news_mysql.lib.php');
$id = $_GET['id'];
$news_item = get_news_item($id);
if (count($news_item) <= 0) {
        echo '<h1>Error</h1>'; // deal with the error
} else {
        echo '<h1>' . $news_item['headline'] . '</h1>';
        echo '<h4>' . $news_item['date'] . '</h4>';
        echo '<p>' . $news_item['body'] . '</p>';
}

// and so on...

This works great for UPDATE, INSERT and DELETE statements, but not so
well for SELECT statements that return more than one row.

The first problem is that mysql_fetch_array() works so well for stepping
through the mysql result set, but I don't know how to duplicate it's
functionality in a library function.

The second problem is that even though I might SELECT information from
the same table in a database several times in my application, each time
the query might be a little different. Sometimes I want the information
sorted in a certain way, for example. So I don't want to use a bunch of
bit flags to identify exactly which information I need.

The best I can come up with is to make an exception for the one function
that will return more than one record, and get it to accept an SQL
string as input (instead of a couple parameters), to return a mysql
resource identifier, and then to use mysql_fetch_array on that resource
identifier and step through the results in a while loop as I would
normally.

What's the better way of doing this?

Thank you,

Beau



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to