Chapter 6. Regular Expressions
In the example in the last chapter exampleâa web form that lets users run SQL against a MySQL databaseâyou did one of the most common things programmers do. You wrote code that solves a problem, but itâs ugly, messy, and a little hard to understand. Unfortunately, most programmers leave code in that state. Thatâs something you want to avoid.
Bad code is like sloppy plumbing or a poorly constructed house frame. At some point, things are going to go bad, and someone is going to have to fix problems. And, if youâve ever had an electrician tell you what he has to charge you because the guy who did the work initially did it wrong before, you know how expensive it is to fix someone elseâs mistakes.
But hereâs the thing: Even good code is going to fail at some point. Any time you have a system that involves humans, at some point, someone will do something unexpected, or maybe just something you never thought about dealing with when you wrote your code. And thatâs when youâre the electrician, trying to fix things when the customerâs unhappyâbut in this scenario, thereâs nobody else to blame.
So, writing ugly code that works really isnât an option. At the moment, the code in run_query.php right now is very ugly. Itâs all those if
statements that are trying to figure out whether the user entered a CREATE or an UPDATE or an INSERT, or maybe a SELECTâ¦or who knows what else? What you really need is a way to search the incoming query ...
Get PHP & MySQL: The Missing Manual, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.