Archive for the 'PHP' Category

Useless use of if award

Friday, July 21st, 2006

Similar to the useless use of cat award. The useless use of if award highlights code examples where people use the if function or ternary operator when the return of the expression does the exact same thing. I first noticed this with returning boolean values from php functions. To protect the innocent the winner of today’s award will remain anonymous.

<anonymous> to my knowledge, youll have to SUM(IF(your_field <> “”, 1, 0)) as total_non_empty

Ignoring that the whole query should be using where your_field != ” and group by the non if() way to write this is:

SUM(your_field <> ”)

These examples aren’t an award to a specific person since I’m digging them up from my memory. This pretty much applies to both C and PHP.

< ?php

function foo()

{

$str = 'foo';

return $str == 'foo' ? TRUE : FALSE;
}

?>

Can be written as

< ?php

function foo()

{

$str = 'foo';

return $str == 'foo';
}

?>

I hope this helps you save a few keystrokes when writing code in the future.

How to get your question answered on irc by example

Sunday, May 29th, 2005

n0other: Hello, I have a field publish_to, I want to select only those entries, where publish_to is not older than today, publish_to contains for example ‘2005-05-30′, I do: SELECT * FROM table WHERE (publish_to - “‘.date(’Y-m-d’) .’”) > 0; (php date function), but that doesnt work, how should I implement this, I cannot change date structure

Notice the detailed question with example. This shows people exactly what the problem is and that you have taken the time to try to solve it yourself.

firewire: so you want where it’s today and newer?
firewire: where publish_to >= curdate()

A quick response prior to testing

n0other: oh :}

Now after a quick test a final response letting everyone know that the problem was solved

n0other: works perfectly, thanks again
firewire: :)

It’s little times like these that make helping people on irc so much fun.

MySQL Authentication Denial

Tuesday, March 29th, 2005

It seems to me that a lot of people have auth denial when trying to make connections to MySQL. They seem to ignore the text of the error message. ‘Access Denied’ means access denied, nothing else.

Remember three things have to match. The host as MySQL sees it, the username, and the password. When MySQL returns access denied it’s not broken. One or more of those three things does not match. I don’t really need to reiterate what’s in the manual. Chang the lock or change the key to make it fit.

Block Comments.

Wednesday, January 19th, 2005

Like many of the things I discover only to find out that somebody else thought of it first is a handy way of blocking out large portions of code for testing.

(more…)

Links and the advent of tabbed browsers.

Saturday, January 15th, 2005

Tabbed browsers have made the target=_new _blank or _top style links annoying and useless. In the old days these links allowed visitors to easily return to your site after viewing a few pages of off site content. Now they are a sneaky surprise that shows up like a hand grenade for those of us that use modern tabbed browsers. There is nothing worse than a link that pops up a new window covering up my existing neatly organized tabs. After reading a few links in this new window I start creating new tabs in it. This leads to having my normal sites open multiple times. What a pain. A good link will give me enough information to decide if I want to choose to open it in a new tab or over the existing one. Having multiple browser windows open is a thing of the past.

Apologies to all of you IE users that have no clue what I’m talking about. Get Firefox! It’s time.