{"id":389,"date":"2007-12-19T21:10:16","date_gmt":"2007-12-20T02:10:16","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/n1zyy\/2007\/12\/19\/subtly-bad-code\/"},"modified":"2007-12-19T21:10:16","modified_gmt":"2007-12-20T02:10:16","slug":"subtly-bad-code","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/n1zyy\/2007\/12\/19\/subtly-bad-code\/","title":{"rendered":"Subtly Bad Code"},"content":{"rendered":"<p>Alright, let&#8217;s have a little fun&#8230; I just added a new blog and went to include it on the main page, but my code failed citing the database throwing errors. It took me forever to find. I&#8217;m curious if others can find it.<\/p>\n<p>I was further confused because the code worked fine until I added the new blog to the list of ones for it to use, and it was specifically built so that it wouldn&#8217;t matter how many blogs there were. It has a separate file that just lists blogs to include, and reads that file at runtime and builds a query to retrieve posts from all of them.<\/p>\n<p>You need some background, first&#8230; All the posts are stored in a database, so each has its own table. I built this monster query, basically looking something like (Get most recent posts from blog 1) UNION (Get more recent posts from blog 2) UNION (&#8230;3&#8230;), and then tack an &#8220;ORDER BY&#8230;&#8221; onto the end. Credit for this idea goes to Andrew; I&#8217;d have never thought of it myself.<\/p>\n<p>What the list includes is blog IDs in the database. They ranged from 2 to 9, skipping 8 (which isn&#8217;t used). After a bout of spam registrations, the numbers got run up, so when I included the new one, it was numbered 51.<\/p>\n<p>The below code (in PHP) calls some custom-rolled functions, but I&#8217;ll just say up front that the error does not depend on understanding how they work. Similarly, the answer does not have to do with caching in any way, so don&#8217;t get too hung up on the amount of code devoted to working with the cache. (And finally, I&#8217;m building one huge variable called $query the whole time, and then return that variable&#8230; This isn&#8217;t a crucial thing to understand either, I just wanted to explain it since it&#8217;s somewhat of a bizarre practice. .= is the PHP variable concatenation method.)<\/p>\n<blockquote>\n<pre>\n\/\/ $count is the number of blogs to pull out\nfunction genRPQuery($count) {\n  \/\/ Retrieve it from Memcache\n  $query = getCachedObject(\"bigquery-$count\");\n  \/\/ It'll return NULL if it doesn't exist, so we check for that...\n  if($query) return $query;\n\n    \/\/ Since we're here, we didn't return, and\n    \/\/ thus didn't get it out of the cache\n\n    \/\/ Next two lines read in the files. blogList()\n    \/\/ returns a list of the blogs -- it's little more than a\n    \/\/ file read with caching enabled.\n    $blogs = explode(',', rtrim(blogList(),\"n\"));\n    $fields = rtrim(cachedFile('.\/fields.inc',30), \"n\");\n\n    foreach ($blogs as $i) {\n      \/\/ We have a loop for each blog\n      \/\/ For unfamiliar eyes, .= is PHP's means of variable concatenation\n      \/\/ We're building a ridiculously-long query, each one a SELECT, encased in\n      \/\/ parens, and we UNION them all together...\n      $query .= \"(SELECT $fields FROM wp_\" . $i . \"_posts WHERE post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT $count)n\";\n      \/\/ If we're not on the last one, insert a \"UNION\" in (see above)\n      if($i<sizeof($blogs)) $query .= \"UNIONn\";\n    }\n    \/\/ Done, so now we order them, getting just the most recent $count ones\n    $query .= \"ORDER BY post_date DESC LIMIT $count;\";\n\n    \/\/ Insert it into the cache for 15 seconds for next time!\n    cacheObject(\"bigquery-$count\", $query, 15);\n    return $query;\n}<\/pre>\n<\/blockquote>\n<p>Remember, it worked fine when the list was blogs number \"2,3,4,5,6,7,9\" but the simple change to \"2,3,4,5,6,7,9,51\" causes it to blow up and try run a query with invalid syntax. This made no sense to me, since the code was built to not care about things like that. I eventually found it, and feel like an idiot.<\/p>\n<p>I've posted a hint in the comments... It's in the interest of fairness because I turned on some debugging and got the information I share. But it also really narrows your attention to a couple of lines, so I don't want to include it in the main post.<\/p>","protected":false},"excerpt":{"rendered":"<p>Alright, let&#8217;s have a little fun&#8230; I just added a new blog and went to include it on the main page, but my code failed citing the database throwing errors. It took me forever to find. I&#8217;m curious if others &hellip; <a href=\"https:\/\/blogs.n1zyy.com\/n1zyy\/2007\/12\/19\/subtly-bad-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,11,22],"tags":[],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-computers","category-insanity","category-programming"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}