The MySQL Wrapper Wrapper

« Return to Article
  • henke37 2012-07-12 09:15
    I don't see the issue. Sure there was a misspelling. And sure the comments might be incorrect.

    But it has logic behind it and no insane syntax tricks. The code is fine.
  • qeury farie 2012-07-12 09:16
    I wrote this: Query is intentionally spelled not good for the obsfucation purpose and the securities.
  • RuBen 2012-07-12 09:16
    I thought that the logAllQeurys() function would log all qeuries, but according to the comment, it's logging all the queries..

    I wasn't expecting that!
  • Jimmee 2012-07-12 09:17
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Of course, this one isn't done right. But still, he's half way up the first step of the ladder, at least he recognises the need.
  • qeury farie 2012-07-12 09:18
    I post again to agree. captcha = dolor. My custom CMS will make me a lot of dolor.
  • Obvious Guy 2012-07-12 09:22
    I'm pretty sure qeury is only consistently misspelt so as to not clash with the name of the function, that is, query.
  • Furiant 2012-07-12 09:23
    Whatever you're paying this guy, I'll dubble it.
  • Daniel 2012-07-12 09:25
    I agree, that is if we are using the defintion of dolor (and not a mispelling): http://www.medterms.com/script/main/art.asp?articlekey=24911
  • Francesco 2012-07-12 09:28

    Geez, what does people expect to find when posting job openings for a PHP project?
    Programmers?
  • dgvid 2012-07-12 09:31
    Francesco:

    Geez, what does people expect to find when posting job openings for a PHP project?
    Programmers?

    Right? Isn't this code about par for the coarse ;) for PHP programmers?
  • Manadar 2012-07-12 09:33
    /* You fail */ SELECT 1 + 1
  • RHuckster 2012-07-12 09:39
    Wow, it's still June? I wish I were a little kid because my summer vacation would have just extended an extra month!
  • q.kontinuum 2012-07-12 09:47
    [quote user=RHuckster]Wow, it's still June?[/quote]

    Even June, Friday 13th, so it's still 2008, 2003 or even earlier ;-) Don't know how old you will be in the far away Friday 13th of July 2012, but maybe now (2003? 2008?) you are still a lucky child?
  • steenbergh 2012-07-12 09:49
    It's planned for Friday the 13th, something was bound to go wrong...
    BTW, doews MySQL support the WITH clause? Because that would throw off his 'select' substring a little.
  • Cbuttius 2012-07-12 09:53
    the way the weather is here in London you wouldn't think it was July or June. More like March or April

  • Cbuttius 2012-07-12 09:57
    [quote user="q.kontinuum"][quote user=RHuckster]Wow, it's still June?[/quote]

    Even June, Friday 13th, so it's still 2008, 2003 or even earlier ;-) Don't know how old you will be in the far away Friday 13th of July 2012, but maybe now (2003? 2008?) you are still a lucky child?
    [/quote]

    During my lifetime any of 1969, 1975, 1980, 1986. 1997, 2003 or 2008. In future years 2014, 2025, 2031, 2036, 2042 and if I live that long 2053.
  • Sal Paradise 2012-07-12 09:57
    Lame. Crying over misspelled words and lack of error logging does not make a WTF.
  • Moi 2012-07-12 10:01
    I bet the code come from France. Not that french are inherently bad programmer, but because the broken english sound very much like french going through google translate.

    Then again, broken english comment may not be the main problem in this code.
  • foo 2012-07-12 10:15
    Moi:
    I bet the code come from France. Not that french are inherently bad programmer, but because the broken english sound very much like french going through google translate.

    Then again, broken english comment may not be the main problem in this code.
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.
  • pantsman 2012-07-12 10:27
    You deserve this if you use PHP.
  • PiisAWheeL 2012-07-12 10:30
    It makes my eyes bleed...
  • Drtfsxzjkl 2012-07-12 10:31
    Hey Pittsburgh Readers – who's up for grabbing a couple drinks tomorrow, Friday (June 13) Night at Market Square? Mark, Remy, and I will be there -- just drop me a line and we'll catch up.

    Despite having seen it so many times I still can't wrap my head around the fact that people actually do this (meeting with people you don't know and "grabbing drinks" and chatting) and they enjoy it. I can barely muster enough willforce to greet other people in the morning.
  • Anonymous 2012-07-12 10:42
    Drtfsxzjkl:
    Despite having seen it so many times I still can't wrap my head around the fact that people actually do this (meeting with people you don't know and "grabbing drinks" and chatting) and they enjoy it. I can barely muster enough willforce to greet other people in the morning.


    Maybe that's why it's held at night. :)
  • FragFrog 2012-07-12 10:43
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Of course, this one isn't done right. But still, he's half way up the first step of the ladder, at least he recognises the need.

    Agreed. The original MySQL wrapper in PHP is clumsy in use to say the least, although the mysqli and PDO classes try to remedy that.

    Almost everything we see in this function (result testing, connection testing, looping over the resultset, getting insert ID's, etc) is common code. Most of this is required for every query you execute. Putting all that in a wrapper so you don't have to copy-paste it time and again is, if anything, good coding practice. And make no mistake: this is a pretty simple way to do so - I have seen versions of mysql wrappers requiring a thousand lines of undocumented code. In comparison, this is neatly done indeed.

    Of course, there are a number of problems with it (lack of flexibility, unpredictable behavior, etc), but the basic idea of a simple method to handle all that tedious default stuff concerning database interaction in PHP, is far from a WTF.
  • PiisAWheeL 2012-07-12 10:50
    FragFrog:
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Of course, this one isn't done right. But still, he's half way up the first step of the ladder, at least he recognises the need.

    Agreed. The original MySQL wrapper in PHP is clumsy in use to say the least, although the mysqli and PDO classes try to remedy that.

    Almost everything we see in this function (result testing, connection testing, looping over the resultset, getting insert ID's, etc) is common code. Most of this is required for every query you execute. Putting all that in a wrapper so you don't have to copy-paste it time and again is, if anything, good coding practice. And make no mistake: this is a pretty simple way to do so - I have seen versions of mysql wrappers requiring a thousand lines of undocumented code. In comparison, this is neatly done indeed.

    Of course, there are a number of problems with it (lack of flexibility, unpredictable behavior, etc), but the basic idea of a simple method to handle all that tedious default stuff concerning database interaction in PHP, is far from a WTF.
    A cow in a dress is still a cow.
  • Dave 2012-07-12 10:51
    RuBen:
    I thought that the logAllQeurys() function would log all qeuries, but according to the comment, it's logging all the queries..

    I wasn't expecting that!


    Incorrect! It log's all hi's query's!
  • Dave 2012-07-12 10:54
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions


    What is inherently wrong with it, buddy, is that it has already been written for you. You've heard of PDO, surely? And if you don't like that, every single damn framework has its own wrapper already.

    A developer should be given a good slapping for attempting to write their own wrapper.
  • Dave 2012-07-12 10:55
    Sal Paradise:
    Lame. Crying over misspelled words and lack of error logging does not make a WTF.


    Do you really think that's all that's wrong with this code, or are you trolling?
  • Moi 2012-07-12 10:58
    foo:
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.


    WHile qeury would be kind of a common mistake in french (the u being totally silent), the specific phrase I was refering to is :

    ///loop through the result and full the array

    It does not make any sense in english. But it is a blind idiot translation of the french phrase. Maybe other language too, but being hideously bad at english is a common quirk of french software engineer, since english comment are mandatory but there is no other reason to learn english.
  • Zylon 2012-07-12 11:03
    PiisAWheeL:
    A cow in a dress is still a cow.

    But with a wrapper function you don't have to dress the cow yourself.
  • Chuck Lester 2012-07-12 11:12
    Moi:
    foo:
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.


    WHile qeury would be kind of a common mistake in french (the u being totally silent), the specific phrase I was refering to is :

    ///loop through the result and full the array

    It does not make any sense in english. But it is a blind idiot translation of the french phrase. Maybe other language too, but being hideously bad at english is a common quirk of french software engineer, since english comment are mandatory but there is no other reason to learn english.


    Seems to me it could also be spanish ...
  • Ouhlala 2012-07-12 11:14
    ///loop through the result and full the array

    It does not make any sense in english. But it is a blind idiot translation of the french phrase. Maybe other language too, but being hideously bad at english is a common quirk of french software engineer, since english comment are mandatory but there is no other reason to learn english.

    Actually, I'm going to go on a limb and say that you are French yourself and you believe that the word the guy wanted to use is "remplir" (fill in English); however, I would think it is more likely to be a typo in this case: see how the U is close to the I on your keyboard and it becomes
    ///loop through the result and fill the array

    which is perfectly goo.. err, OK English... :)
  • Remy Porter 2012-07-12 11:17
    I definitely have my own personal limits on sociability, but for me it's more of a "hang out with folks once or twice every few months".

    At this point, I'm more worried that I'm going to completely forget and be halfway home before I remember I was planning to meet people after work.
  • Jabba the Hutt 2012-07-12 11:18
    Dave:
    You've heard of PDO, surely? And if you don't like that, every single damn framework has its own wrapper already.


    He was creating his own CMS; he obviously can't use someone else's framework because the superiority of his own is his competitive advantage. Mandatory Joel link.

    I wonder if this is a theme among PHP developers. It would not be the first time I've seen one brag about their own awesome CMS. Maybe it is like how template engines proliferate in Python because they are so easy to create.
  • Ozzy 2012-07-12 11:45
    TRWTF is PHP.
  • Meep 2012-07-12 11:53
    foo:
    Moi:
    I bet the code come from France. Not that french are inherently bad programmer, but because the broken english sound very much like french going through google translate.

    Then again, broken english comment may not be the main problem in this code.
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.


    That's true as a rule, but in modern English orthography, 'qu' is very common and associated with the 'kw' consonant sound, so it's not really a vowel confusion, and I'm doubtful that it's accidental.

    Someone suggested he deliberately misspelled it to differentiate it from a library term; that would be TRWTF if that's the case since that's a horrible convention.
  • Meep 2012-07-12 12:00
    Jabba the Hutt:
    Dave:
    You've heard of PDO, surely? And if you don't like that, every single damn framework has its own wrapper already.


    He was creating his own CMS; he obviously can't use someone else's framework because the superiority of his own is his competitive advantage. Mandatory Joel link.

    I wonder if this is a theme among PHP developers. It would not be the first time I've seen one brag about their own awesome CMS. Maybe it is like how template engines proliferate in Python because they are so easy to create.


    Joel's got a point... I have no idea about PHP developers, but one thing about web frameworks and web stuff in general is how awful it tends to be. If I were going to hire someone to work on an existing web framework, if he's already written one himself, even if it sucked, at least it means he knows how one works and has already learned some lessons about it.
  • Meep 2012-07-12 12:11
    Ouhlala:
    ///loop through the result and full the array

    It does not make any sense in english. But it is a blind idiot translation of the french phrase. Maybe other language too, but being hideously bad at english is a common quirk of french software engineer, since english comment are mandatory but there is no other reason to learn english.

    Actually, I'm going to go on a limb and say that you are French yourself and you believe that the word the guy wanted to use is "remplir" (fill in English); however, I would think it is more likely to be a typo in this case: see how the U is close to the I on your keyboard and it becomes
    ///loop through the result and fill the array

    which is perfectly goo.. err, OK English... :)


    Okay? Aside from dropping punctuation and capitalization, that is perfect American Standard English. All of the comments are fragments without capitalization or punctuation, which is appropriate for comments. As far as fragments can be, they're grammatically perfect. The misspellings all look like typos, except dubble* looks like a learned mistake, he probably inferred it (quite logically) from bubble. Most people have that; I can't, for some reason, get "receive" right without checking it, and always want to pronounce the 'ch' in "chimera" as a "sh".
  • dogmatic 2012-07-12 12:25
    Dave:
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions


    What is inherently wrong with it, buddy, is that it has already been written for you. You've heard of PDO, surely? And if you don't like that, every single damn framework has its own wrapper already.

    A developer should be given a good slapping for attempting to write their own wrapper.


    Frameworks create unnecessary overhead, and unless you find in planning that you will need many of a framework's features in any particular project they should be avoided. I've found that often a framework's promise of saving development time is mitigated by the time it takes to get around a framework when it fails to provide needed functionalities. Often framework's are like learning a new language to do something that would be just as easy in straight PHP.

    PDO is great but it also requires access to the server to install the drivers, thus making your code a lot less portable, so not necessarily the best choice for a CMS.

    That being said this guy's approach is not great. Better to make one class that provides all the methods needed to read and write to the database. This class can contain normal mysqli statements and will be the abstraction layer in case you ever need to use something other than mysql. You will have to make sure that all user input gets processed to prevent mysql injection of course.
  • David 2012-07-12 12:31
    We had someone write a similar (in some ways) wrapper around the standard Python DB query; like this one, it would sometimes return an array of results, and sometimes just a single result (which was itself an array). I'm not sure we ever did convince him that a predictable function return-type is a good idea, but we *did* make him fix it. :)
  • Jack 2012-07-12 12:55
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Of course, this one isn't done right. But still, he's half way up the first step of the ladder, at least he recognises the need.


    No, he doesn't recognize the need. He's a cargo cult programmer. All he knows is "good coders create wrapper classes," and he assumes that if he creates a wrapper, his code will be good. He has no idea why creating a wrapper is good (in some cases), so he goes about it all wrong.

  • Nagesh 2012-07-12 13:38
    There are no bad languages. Bad programmers only. and bad programmers can ruin good language like java also, so PHP is not exception to this.


    -A good programmer.
  • foo 2012-07-12 13:45
    Moi:
    foo:
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.


    WHile qeury would be kind of a common mistake in french (the u being totally silent),
    But "eu" is pronounced quite differently from "e" (or "ue") in French.
  • jMerliN 2012-07-12 14:02
    It seems to be a common theme these days with people just learning PHP. "I'm going to make my own CMS." Some even claim they're going to start a company with it.

    I just point them at the existing mature, debugged projects that have active developers maintaining them and support and say "the only value in writing your own CMS is to learn what not to do, you don't really stand a chance at competing with existing solutions."

    Though I like taking their initiative to create something and using it as a tool to force them to learn how to do things the right way. I ask them what happens if I am evil and I input bad stuff here? You have a box to enter HTML, what if I enter a script tag that steals information from a client's computer or if I embed image tags that don't go to this domain (http://www.somesitewithCSRFvulnerability.com/admin/do/sendUserDatabase/to/evilbadguy@gmail.com). What if I use "scary" characters in my query, like quotes? What if I try to write a blog post in Chinese? Why does this script have a state-changing side effect when I issue a GET request on it? You have an XHR here to get the blog content to display, how are you going to make this crawler-friendly so blogs in your CMS are indexable by search engines? What's stopping me from uploading a PHP file when you expect an HTML file? What happens when a bad guy compromises your software and gets access to the database, can he see my password?

    Then I'll introduce them to how we have caching networks built to make the internet faster and ask them why their PHP pages return a 200 with cache control (or worse, a cache control of no-cache, no-store, max-age=0). Then I'll ask them how they can use cache control headers properly to make their site more efficient. I'll ask why are you using jQuery and hosting it locally when Google hosts it on a CDN and everyone who's ever been to YouTube will likely have it cached for 1 year in their browser?

    And on and on. Needless to say, there's a LOT of stuff to consider when making any decent web application especially something as involved as a really good CMS. I would agree that "I'm going to start a company with my own CMS and it's going to be better than Joomla and Drupal" is a big red flag unless this person has serious credentials to back that up.
  • Joe 2012-07-12 14:14
    Jabba the Hutt:

    I wonder if this is a theme among PHP developers. It would not be the first time I've seen one brag about their own awesome CMS. Maybe it is like how template engines proliferate in Python because they are so easy to create.


    Yes, writing their own CMS is the "JAPH" of PHP. Everybody has to do it. Except that nobody's a good enough PHP programmer to fit it into a .sig

    --Joe
  • no laughing matter 2012-07-12 15:05
    Jabba the Hutt:

    He was creating his own CMS; he obviously can't use someone else's framework because the superiority of his own is his competitive advantage. Mandatory Joel link.

    Well there is one good advice in there:
    Joel Spolsky:
    If it's a core business function -- do it yourself, no matter what.


    Unfortunately the advice that is missing is:
    not Joel Spolsky:
    If it's not a core business function and there are mature, tested solutions available -- don't do it yourself, or your code will be the real WTF!


    And that is the Worse-Than-Failure that inevitably folllowed.

  • Jazz 2012-07-12 15:31
    FragFrog:
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Of course, this one isn't done right. But still, he's half way up the first step of the ladder, at least he recognises the need.

    Agreed. The original MySQL wrapper in PHP is clumsy in use to say the least, although the mysqli and PDO classes try to remedy that.

    Almost everything we see in this function (result testing, connection testing, looping over the resultset, getting insert ID's, etc) is common code. Most of this is required for every query you execute. Putting all that in a wrapper so you don't have to copy-paste it time and again is, if anything, good coding practice. And make no mistake: this is a pretty simple way to do so - I have seen versions of mysql wrappers requiring a thousand lines of undocumented code. In comparison, this is neatly done indeed.

    Of course, there are a number of problems with it (lack of flexibility, unpredictable behavior, etc), but the basic idea of a simple method to handle all that tedious default stuff concerning database interaction in PHP, is far from a WTF.


    Using an abstraction layer is good; using a single function for your entire abstraction layer is bad. If the original coder had paid attention to any of the principles of good usage of PHP, I would agree that this is far from a WTF; but the complete lack of object-orientation and typing discipline push it solidly back into WTF territory.

    These days there are a lot of Object-Relational Mapping frameworks available for PHP: Doctrine, RedBean, Propel. Every serious PHP programmer I know either uses an off-the-shelf ORM solution, or makes their own by wrapping some objects around Zend Framework or PDO statements. No serious PHP programmer would use the above function for this.
  • Roujo 2012-07-12 15:39
    qeury farie:
    I post again to agree. captcha = dolor. My custom CMS will make me a lot of dolor.


    Appropriately enough, "dolor" is Latin for pain. =P
  • da Doctah 2012-07-12 15:41
    foo:
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.
    WHAT?!

    Did you never read Peter Benchley's article on how to speak French? With its table of French vowels for handy reference:

    "a" -> pronounced -> "ong"
    "e" -> pronounced -> "ong"
    "i" -> pronounced -> "ong"
    "o" -> pronounced -> "ong"
    "u" -> pronounced -> "ong"

    Add to this the impossible-to-ignore fact that all French consonants are silent, and the profusion of flyspecks that sprout from every letter. (Most French people don't even realize that the Vietnamese, their former colonials, have perpetrated a hilarious satire on the latter, with an average of 3.2 accent marks per letter.)

    Foriegn spelling is wierd.
  • Jockamo 2012-07-12 16:41
    We get it, you fat nerds don't like php. You've made that quite clear...
  • 'Sam 'Smith 2012-07-12 17:35
    dogmatic:
    framework's are like learning a new language
    Thi's i's 'so true! 'So many folk's 'suppo'se language i's 'simple. 'Still, if there i's 'some trivial 'syntax rule you mi's's, or ju'st don't under'stand, you end up looking 'so 'stupid!
  • Someone, but not the other someone 2012-07-12 18:11
    Uhm....(leaving aside the rest of this WTF) the last comment about the consistent misspelling of Query....
    While it grates me to see qeury instead of query, one of the most important principles of readable code is consistency (more important to be consistent than correct in choosing conventions and variable names). If anything, this at least is a good thing....
  • Bilongda 2012-07-12 18:14
    Cbuttius:
    the way the weather is here in London you wouldn't think it was July or June. More like March or April

    Er...I thought London only had two weather patterns anyway...."Raining" and "about to rain" - which one is the March or April one?
  • Whosdr 2012-07-12 18:20
    Sorry I'm off-topic, but I just got this in a captcha.

  • phillipe 2012-07-12 18:20
    Meep:
    Ouhlala:
    ///loop through the result and full the array

    It does not make any sense in english. But it is a blind idiot translation of the french phrase. Maybe other language too, but being hideously bad at english is a common quirk of french software engineer, since english comment are mandatory but there is no other reason to learn english.

    Actually, I'm going to go on a limb and say that you are French yourself and you believe that the word the guy wanted to use is "remplir" (fill in English); however, I would think it is more likely to be a typo in this case: see how the U is close to the I on your keyboard and it becomes
    ///loop through the result and fill the array

    which is perfectly goo.. err, OK English... :)


    Okay? Aside from dropping punctuation and capitalization, that is perfect American Standard English. All of the comments are fragments without capitalization or punctuation, which is appropriate for comments. As far as fragments can be, they're grammatically perfect. The misspellings all look like typos, except dubble* looks like a learned mistake, he probably inferred it (quite logically) from bubble. Most people have that; I can't, for some reason, get "receive" right without checking it, and always want to pronounce the 'ch' in "chimera" as a "sh".
    It's funny how on auto-pilot you can make silly mistakes like that....if I'm a little distracted, I often replace A with 8 and vice-versa. Sort of illogical, given other than the sound they are nothing alike, and yet the brain seems to do that sometimes....

    Maybe I gotta give up C and go PHP if my brain is failing me....
  • phillipe 2012-07-12 18:27
    da Doctah:
    foo:
    Actually, this kind of misspelling (confusion of vowels) is more typical of English speakers, esp. American English, where all vowels are pronounced more or less the same.
    WHAT?!

    Did you never read Peter Benchley's article on how to speak French? With its table of French vowels for handy reference:

    "a" -> pronounced -> "ong"
    "e" -> pronounced -> "ong"
    "i" -> pronounced -> "ong"
    "o" -> pronounced -> "ong"
    "u" -> pronounced -> "ong"

    Add to this the impossible-to-ignore fact that all French consonants are silent, and the profusion of flyspecks that sprout from every letter. (Most French people don't even realize that the Vietnamese, their former colonials, have perpetrated a hilarious satire on the latter, with an average of 3.2 accent marks per letter.)

    Foriegn spelling is wierd.
    Oui

    "Damn Foreigners - can't go anywhere in the world without meeting any" - Antony Aloysius St. John Hancock.
  • oheso 2012-07-12 22:42
    Moi:
    but being hideously bad at english is a common quirk of {$NATIONALITY} software engineer ...


    FTFY.
  • Doctor_of_Ineptitude 2012-07-12 23:32
    qeury farie:
    I post again to agree. captcha = dolor. My custom CMS will make me a lot of dolor.


    Qeury Farie, I like you. You are a lot more tolerable than Fake Nagesh and his sock puppets and their sock puppets. As a fake Indian I become bored of all the garbage that Nagesh has spewed. Thanks for being a being of incorrect spelling but somewhat correct grammar.

    Captcha:populus. I hope you are populus too.
  • Brian 2012-07-13 01:13
    I'm clearly very very far in the minority on this, but depending on what kinds of places it's used, I'm kind of okay with the function's return value changing.

    The alternative is to always return an array of arrays, and then to unwrap it after every use that expects a single row. (I can't think of many good options here, other than selecting the most recent x by ordering and limiting 1?)

    When you're doing that, and your query fits your expected pattern, unwrapping the single result is not the most horrible thing to do.

    Yes, it would be better to just have an overload like query_single that unwraps it for you, but for something you knock out quick, because you DON'T want to build an entire framework.. I don't see anything egregious about this.

  • AndyCanfield 2012-07-13 02:31
    There are no bad languages. Bad programmers only. and bad programmers can ruin good language like java also, so PHP is not exception to this.

    I am tired of the anti-PHP thunder. I code in PHP because it's better than the alternatives (C, C++, Java). The anti-PHP pages talk about weird syntax, but you don't need that syntax and live without it. Sure I've got wrappers; about 15,000 lines of wrappers that I've developed over ten years and re-use constantly, including DB.inc, Object.inc, and PickList.inc.
    (FYI I work alone, not on a team. YMMV)
  • Watson 2012-07-13 03:33
    Mistype $qeury once; every time after that it's $q<letautocompletedotherestforme>.
  • qwz 2012-07-13 04:16
    henke37:
    I don't see the issue. Sure there was a misspelling. And sure the comments might be incorrect.

    But it has logic behind it and no insane syntax tricks. The code is fine.


    What if the first word in your select query is WITH?
  • beginner_ 2012-07-13 04:25
    Jabba the Hutt:

    He was creating his own CMS; he obviously can't use someone else's framework because the superiority of his own is his competitive advantage. Mandatory Joel link.


    Don't know what to think of a guy that was responsible for a language that opens modal dialogs to show "compiler" errors.
  • radarbob 2012-07-13 08:54
    Waving the BS Flag on all the "kudos for recognizing the need for a PHP SQL wrapper."

    I know bupkis about PHP but after 2 minutes reading this I can say convincingly "Well, everyone knows PHP needs a good SQL wrapper."

    This "nice try" is a true WTF because a) it is crap yet 2) he put it into production on a "paid gig" which means d) he's unprofessional, incompetent, and probably dyslexic.
  • big picture thinker 2012-07-13 16:32
    no laughing matter:

    Well there is one good advice in there:
    Joel Spolsky:
    If it's a core business function -- do it yourself, no matter what.


    Unfortunately the advice that is missing is:
    not Joel Spolsky:
    If it's not a core business function and there are mature, tested solutions available -- don't do it yourself, or your code will be the real WTF!
    [/url].

    Yes he actually does say that if you scroll down...
  • the irritainer 2012-07-13 18:16
    Trfwtf is not that its php or that the code is that bad. Im not saying its great either but this codemonkey at least understands the basic principles on how to deal with queries. Ive seen way worse...

    The real wtf is the submitter for not doing a code review during the interview. Php is a powerfull language in hands of those who know what theyre doing!

    Captcha: i typed this from my new tab, sorry for spelling errors
  • Coyne 2012-07-13 23:48
    Gee, he's invented everything, inclueding his own kewl turmenology.
  • nico 2012-07-14 03:55
    You 'Sir, ju'st made my day.
  • Vincent 2012-07-14 09:15
    TRWTF is that drupal's codebase isn't much better than that
  • SQL Vader 2012-07-14 16:39
    I have wrapped your mysql lib. Pray I don't wrap it any further.
  • John 2012-07-16 03:05
    Did you ever stop to think that maybe this was wrote before PDO or the prevalence of frameworks? There was a point in time where you pretty much had to if you wanted something sane to work with. However, if this was wrote in the last several years I'll agree with you. Even if it wasn't the developer needs to be slapped for doing such a shitty job.
  • iWantToKeepAnon 2012-07-16 17:21
    This isn't WTF code, this is newb code. Sure it's bad, but not WTF. I've noticed almost all the code snippets recently are not WTF, just bad ... not evil bad. This list is turning into experts looking down their noses at the less advanced programmer.

    Sure a lot of these bad programmer think "mine will be better", but that shows (misplaced) confidence.

    Sigh.
  • noone 2012-07-17 11:39
    Except he was paid for this code so the newb defense is invalid.
  • Thomas 2012-07-31 09:50
    Manadar:
    /* You fail */ SELECT 1 + 1


    (SELECT 1 + 1) UNION (SELECT 1 - 1)

    Brian:
    When you're doing that, and your query fits your expected pattern, unwrapping the single result is not the most horrible thing to do.


    But if won't work as you'd expect with LIMIT 5,1.
  • Daniel15 2012-08-08 22:46
    Jimmee:
    There is nothing inherently wrong with creating a wrapper around the PHP mysql functions, indeed, done right it's pretty essential in the manner of creating an abstraction layer to allow you to create a lot of convenience and safety - even if you never forsee using anything except mysql.

    Except a wrapper already exists and is built-in to PHP. It's called PDO.
  • Richard 2013-06-13 12:25
    I just fixed a bug in another part of the application.

    Because

    $query = "SELECT `worked_hours_id`
    FROM `worked_hours`
    WHERE
    `wh_date` = '" . $thisDay . "'
    AND `display_row` = " . $rowID . "
    AND `users_id`=" . $userid . "
    LIMIT 1;";
    $result = $mysql->query($query);

    if ( isset($result->worked_hours_id) )
    {
    // real important stuff here
    }


    did not trigger the if() statement because


    if(substr( strtolower($qeury) ,-7) == 'limit 1' && $resultArray){
    $resultArray = $resultArray[0];
    }


    did not trigger because of the f*cking semicolon at the end of the query. Ehr qeury.
    I'm going to cry in a corner now.