The PHP WTF

    Harry Fuecks
    Share

    This site keeps popping up on my radar recently, inspired by this site.

    Although things like this are quite amusing, got mixed feelings about the idea in general. On the one hand anything that helps people improve their coding skills is a good thing. On the other hand, think the approach being taken here is a kind of elitism which will discourage asking of “dumb” questions; that the overall effect is negative. Ignorance can (and should) be forgiven in programming, IMO – no one knows it all.

    More specifically this particular PHP WTF raises my doubts further – reading it you might get the idea that mysql_num_rows() is a bad idea period, which isn’t the case. There are situations where it has a value in determining the size of a result set you are about to use but reading this you might come the conclusion that the right solution is always to use two queries like;


    $query = 'SELECT COUNT(*) FROM sometable';
    $result = mysql_query($query,$connection);
    list($count) = mysql_fetch_array($result);

    $query = 'SELECT * FROM sometable';
    $result = mysql_query($query,$connection);
    while ( $row = mysql_fetch_array($result) ) {
    // etc.
    }

    The other, much bigger problem, that was missed in the discussion was the impact of ‘SELECT * FROM sometable’ – a full table scan. If possible, would be nice to narrow down the result set with some WHERE clauses and take advantage of indexes. There’s a good discussion here (Google cache) in MySQL Performance Tuning from The Man.

    That said, think this WTF was better argued.