Making sure your updates work

Working on mokocharlie.com is great. I get to exercise as much PHP muscle as I can on the project and get away with it because it’s considered progress and innovation (at least within the scope of the project). I find myself doing a lot of database work and hence updates and inserts and that sort of thing.

Now I have been doing PHP for a while now and for it’s shortcomings as compared to say python, it is quite convenient as a web scripting language. In other words you can get a lot of things done quickly without planning too much.

I have come to discover something while working on this project about certain nuances with the mysql wrapper in PHP which i think i should point out, now before any comments I would like to say that I know this is my opinion and I do understand why it’s implemented this way in the first place. I believe that when an UPDATE query is run within PHP just as other queries should return true or false based on success or failure respectively, I believe updates should return false as well if no rows are affected. I haven’t yet thought of the can or worms that that might open but it will save a lot of people a lot of embarrassment. Or perhaps it will allow a lot of PHP developers to write worse code. However you chose to look at it here’s is an example of updating a users password in a scenario where they have forgotten it or have requested to change it.

When an update is run it always returns true, unless there was a syntax or naming error somewhere in the query, so that

/*abridged for brevity */

   2:  $sql = “UPDATE `mytable` SET `mypassword` to ‘$newencryptedpassword’
WHERE `id` = `myid` and `email` = ‘$myemail’”;
   3:  if(mysql_query($sql)){
   4:  // means query was successful????
   5:  }else{
   6:  // there was a mysql_error()???
   7:  }

Now this, provided those variables and parameters are correct would return true all the time I have realised however that this might not always be the most desirable effect, as you might end up with a type 2 error for those of you savvy to the little trickeries of statistics.

to avoid this you will need to actually alter this little snippet to read like this.

   1:  $sql = “UPDATE `mytable` SET `mypassword` to ‘$newencryptedpassword’
WHERE `id` = `myid` and `email` = ‘$myemail’”;
   2:  if(mysql_query($sql) && mysql_affected_rows() > 0){
   3:  // now this should really indicate that it was successful
   4:  }else{
   5:  //query failed
   6:  }

That should give you a means of being sure that the query actually did something and just doesn’t return true.

Netbeans and PHP

I never thought I’d be talking about using such a heavyweight IDE to write code in a scripting language. After the first iteration of a project I’m actively working on, I thought I should set up some versioning for this project, prompting me to go through the subversion server set up on my home Ubuntu Hardy server following these instructions.

After this I wanted an IDE that had subversion support and my regular Komodo-IDE was not cutting it anymore. I had heard there were extensions and so on. But with svn you usually want something baked into the whole shebang!!!!

So enter Netbeans -  I had tried it earlier (6.1 i think) on and the PHP support was flaky at best. 6.5 however did a better job at supporting PHP. I discovered this using netbeans for my MSc Dissertation when I needed to use the SOAP – UI plugin to test SOAP – Based web services, i stumbled on support for PHP projects.

I must say besides a few annoying quirks like Netbeans showing me warnings I can’t filter out in the output window it is a pretty solid IDE. A bit heavy on the memory consumption side than say Bluefish, or gPHPEdit, or even :::sigh::: gecko powered komodo but I think I will be using it for a while to see how well it does.

On a related note:

So after a few weeks of tinkering and reading up on the facebook API we finally have a workable facebook/mokocharlie application. It basically takes any picture you have on facebook (preferably a portrait picture) and makes it into a magazine cover. Then saves it back to any other application you want.

The was written with a lot of PHP and the GD image manipulation library.

Give it a whirl and let’s know what you think. You can also post comments on the mokocharlie group page telling us what you think about it.