« Progress Report | Main | Back to College »

January 10, 2004

More About the PHP/MySQL Annoyance

In a comment for "Progress Report", Josh said:

This pertains to your "annoyance".

I looked at my code today, and I see that I haven't done anything to add or strip slashes. So basically, as I've said before, PHP and mySQL works happily. So here's what I do.

When I input something into the database, PHP has already added the slashes. I have a verification page, in which I do a stripslashes on the passed variable. However, the database gets all the slashes and so forth created by PHP.

I realized today that I never dealt with this "annoyance" whatsoever, other than the fact that I don't remember at all when I even had this problem in my mind (even though I should have, like a good little programmer, such as yourself). But yeah, it's not a server thing or whatever (I was full crap), it is a PHP thing. It's not really an annoyance, but they just do it for you.

So here's my thing on the "annoyance". PHP adds the slashes, and mySQL just gladly accepts the incoming data (whatever it may be). When PHP drags that information out from the databse, PHP does it all on it's own, so that the user or the programmer doesn't have to worry about stripping or adding slashes. I swear PHP/MySQL must live as one! WOOOOOO!

The first point that I would like to make is that not all servers have PHP configured like Josh's server. For example, the Living Water server is not configured to automatically add slashes to inputted data, and I cannot (at least on my own) change this aspect of the configuration. As a result, I manually add slashes to everything. However, the Living Water server does automatically remove slashes, so I must not remove slashes when retrieving data from MySQL or else I might take away slashes that are part of the data. It appears to me that there are instances where even Josh would need to add slashes manually. For example, when Josh retrieves data from MySQL, the slashes are removed. If Josh subsequently wants to insert this data into some other part of the MySQL database (a procedure frequently used to associate data from different tables), he will have to manually add slashes.

However, the important issue here is not related to these details. The important issue is code portability. The open source movement is based on the fact that the same code does the same thing on different computers. Configuration issues like this slash "problem" mean that in order to write truly portable code, one must check the server configuration and respond accordingly every time that data is inserted into a database. Although a function can be written to handle this issue, it adds unnecessary complexity. After I created the Living Water login system, Josh told that the nice thing is that I can now use the same code for different projects. Some of that "niceness" is lost due to issues like this one.

Having said this, I understand the points that Josh makes about the convenience of having PHP do the slash work for you. In fact, much of my emotion on this issue probably arises from the fact that until recently, I did not fully-understand how PHP handles slashes, so I spent much time fixing code that wasn't quite right.

Posted by doshirj at January 10, 2004 01:24 AM

Comments