Finally got GD working with PHP under osX

Since time immemorial, I have been having problems with the php installation that came on my powerbook, striving and straining to get any new modules installed, to make it work in the ways that even the most simpleminded linux install does out of the box, generally frustrated with it. Currently I have two for pay projects that require me to use the GD library, so I broke down and really attacked it today. After about three tries, I finally got something working… specifically I used the entropy install of php, and got it to actually work by converting apache2 from a fat file to a 32 bit only binary based on the instructions from the same site. These instructions were NOT easy to find, and several google searches didn’t turn them up at any point. I only found them after reading of the trials and tribulations that the blogger at #| had with the same problem.
The good news is that it’s done now, and I’m happy.

Selecting the newest row in one-to-many on mySQL

This one was quite a sticky wicket. So what if you have two tables in mySQL, with a one-to-many relationship (say a forum and comments in that forum), and each of the comments is dated. Now let’s say you want a result set containing each forum’s name, and with it the text of the newest comment in that forum.

The obvious way to do this is with a subselect, which mySQL doesn’t have. So how does one do it?

At first I thought that it might be impossible, but I have figured out the answer:

SELECT f.forum_id,
       max(c.created_on) as last_date,
       c2.created_on as created_on,
FROM   forum f
       LEFT JOIN comment c ON (c.forum_id = f.forum_id)
       LEFT JOIN comment c2 ON (f.forum_id = c2.forum_id) 
GROUP BY f.forum_id, c2.comment_id
HAVING created_on = last_date OR last_date IS NULL;

See, it’s pretty clever actually, though it leans on the “HAVING” option, which is sort of crappy. The trick is to join the comment table with the forum table twice. One of those joins gets grouped by forum id so that you can use the max function on it. The other doesn’t get grouped (by adding its primary key to the group by clause) so that you can still pull data out of it. Then you use the having clause to find only the row that has the max date.

I am also accepting values with a max value of NULL so that if a forum has no comments, it still comes back in the results.

Hope this helps someone, somewhere.
Happy Hacking

Weird little MySQL error.

So I just moved some code onto a new server, and I’m suddenly getting the warning:

mysql_query(): 14 is not a valid MySQL-Link resource in <bla bla bal> on line 47

A few other people seem to have gotten this error, but no one has posted a solution. (though one guy oh-so-annoyingly posted “I figured it out, so never mind” … Grrr. I mean, if you’re going to post a question, the answer should be in that thread if you ever figure it out….

So as soon as I figure out the answer I’m going to post it here.

I’m back with the solution:

I got clued onto it from this page: . The error is coming because mysql_close was being called by the destructor, and because I was in safe mode the same MySQL resource was being used for each instance. What threw me even more though was that the destructor was being called at all, because I thought I only _had_ one instance. Turns out that there is a spot in my code where I (accidentaly) passed my DB object by value rather than refrence. This made a new copy of the object, which ran mysql_connect again, because it was in safe mode it returened the _same_ refrence. Then the object got unloaded, the destructor ran and closed the refrence, even though there was another instance of the object out there still using the same refrence.


The most Interesting Code I Have Ever Written

I wrote this about a month back, and Honestly blew my own mind with it. Its far from the best code I have ever written, something which is further exacerbated by the fact that I was shooting to keep it short and small, which never makes your code good, I was also aiming not to use anything that is not a core function in PHP. The flip side is that I used a lot of really uncommon php calls that do some very, very cool things. At one point this was an ajax app, but I discovered that it could be smaller and cleaner by just using frames.

This will be the first post in a series where I go through this code and explain it, and some of the cooler calls that it is using, as well as some advanced php and programming concepts. But for now I’m just going to give a one paragraph overview of what the code does and get it into the post. There is one other file which I will also post and explain at some point.

So read On to take a look at the nitty-gritty, and get a feel for what it does in total
Continue reading The most Interesting Code I Have Ever Written