Leif Mortenson wrote:
> Make the code look something like this:
> function handleQueryError($Query, $dbLink, $file, $line) {
> if (DEBUG == true) {
> print ("<b>Query:</b> $Query<br><b>Error:</b> " .
> mysql_error($dbLink) . "<br><b>File: </b>$file <b>Line: </b>$line");
> } else {
> print ("This site is currently undergoing maintenance. Please try
> again later.");
> // Send mail to administrator here.
> }
> }
>
> $Query = "SELECT id, name FROMX item";
> $DatabaseResult = mysql_query($Query, $DatabaseLink);
> if ($DatabaseResult <= 0) {
> handleQueryError($Query, $DatabaseLink, __FILE__, __LINE__);
> } else {
> // Work with the results of query here
> }
(This unintentionally turned into a mini-rant. Sorry)
I'm sure that Leon already has some plans for FreeTrade 2
but I'd like to offer my preferred way:
function doquery($sql) {
global $DatabaseLink;
$response = mysql_query($sql, $DatabaseLink);
if ($response) {
return $response;
}
if (DEBUG) {
print "<br>SQL: $sql\n";
print "<br>Error: " . mysql_error($DatabaseLink) . "\n";
}
// Send mail to administrator here.
return 0;
}
This way offers database abstraction. It gets the dang
$DatabaseLink out of my code. It makes sure that EVERY
query has at least some error handling. You can extend
it to mail an admin or whatever. *I* wouldn't put in a
maintenance message because I think you'll get many of
them and they may come out in the headers, etc. I do
like the __FILE__ and __LINE__ stuff, but maybe it can
be done some way besides putting it in EVERY call, I
hate that.
Then your actual code could be greatly shortened:
$Query = "SELECT id, name FROMX item";
if (!($DatabaseResult = doquery($Query))) {
return 0;
}
// Work with the results of query here
Furthermore, it happens SO often that you only want the
first result from your query that I use a specialized
version of this to reduce my code even further:
function single_fetch($query, $field="all") {
global $LinkIdentifier;
if (DEBUG) {
print "<b>Single Fetch:</b> about to run query <i>"
. $query . "</i><br>\n";
}
$response = mysql_db_query($database,$query,$LinkIdentifier);
if (!$response && DEBUG) {
print "<br>SQL: $sql\n";
print "<br>Error: " . mysql_error($DatabaseLink);
return 0;
}
if (!$response) {
return 0;
}
$hash = mysql_fetch_array($response);
mysql_free_result($response);
if ($field == "all") {
return $hash;
} else {
return $hash[$field];
}
}
Then, big pieces of code shrink into little pieces:
$sku = single_fetch("SELECT * FROM sku"
. " WHERE ID='$sku'");
if (!sku) {
return 0;
}
// Work with the results of query here
Or even:
$itemid = single_fetch("SELECT Item FROM sku"
. " WHERE ID='$sku'", "Item");
if (!$itemid) {
return 0;
}
So, you might be able to tell that keeping my main-line
code very short is important to me. If you think about
how many lines of code that last operation requires in
FreeTrade version 1, and the number of times that there
are blocks of code like that, you might notice that there
is a lot of opportunity for bugs, typos, maintenance, etc.
Not to mention that it hurts my brain to try to comprehend
code that just goes on and on...
I still love FreeTrade but I'm hoping version 2 has much
less wasted lines of code.
--
Paul Chamberlain, [EMAIL PROTECTED]
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Site: http://www.working-dogs.com/freetrade/
Problems?: [EMAIL PROTECTED]