<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Accidental Developer &#187; php</title>
	<atom:link href="http://osric.com/chris/accidental-developer/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://osric.com/chris/accidental-developer</link>
	<description>What if Gregor Samsa awoke a computer programmer?</description>
	<lastBuildDate>Sat, 28 Jan 2012 23:13:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Finally got GD working with PHP under osX</title>
		<link>http://osric.com/chris/accidental-developer/2009/02/finally-got-gd-working-with-php-under-osx/</link>
		<comments>http://osric.com/chris/accidental-developer/2009/02/finally-got-gd-working-with-php-under-osx/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 23:24:58 +0000</pubDate>
		<dc:creator>giblfiz</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[osx]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=155</guid>
		<description><![CDATA[Since time immemorial, I have been having problems with the php installation that came on my powerbook, striving and straining to get any new modules installed, to make it work in the ways that even the most simpleminded linux install does out of the box, generally frustrated with it. Currently I have two for pay [...]]]></description>
			<content:encoded><![CDATA[<p>Since time immemorial, I have been having problems with the php installation that came on my powerbook, striving and straining to get any new modules installed, to make it work in the ways that even the most simpleminded linux install does out of the box, generally frustrated with it. Currently I have two for pay projects that require me to use the GD library, so I broke down and really attacked it today. After about three tries, I finally got something working&#8230; specifically I used the <a href="http://www.entropy.ch/software/macosx/php/">entropy</a> install of php, and got it to actually work by converting apache2 from a fat file to a 32 bit only binary based on the <a href="http://www.entropy.ch/phpbb2/viewtopic.php?t=3074">instructions</a> from the same site. These instructions were NOT easy to find, and several google searches didn&#8217;t turn them up at any point. I only found them after reading of the trials and tribulations that the blogger at <a href="http://hash-pipe.com/2008/08/php-5-and-xdebug-on-os-x-leopard/">#|</a> had with the same problem. <br />The good news is that it&#8217;s done now, and I&#8217;m happy. </p>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2009/02/finally-got-gd-working-with-php-under-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting the newest row in one-to-many on mySQL</title>
		<link>http://osric.com/chris/accidental-developer/2008/11/selecting-the-newest-row-in-one-to-many-on-mysql/</link>
		<comments>http://osric.com/chris/accidental-developer/2008/11/selecting-the-newest-row-in-one-to-many-on-mysql/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 03:23:30 +0000</pubDate>
		<dc:creator>giblfiz</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=98</guid>
		<description><![CDATA[This one was quite a sticky wicket. So what if you have two tables in mySQL, with a one-to-many relationship (say a forum and comments in that forum), and each of the comments is dated. Now let&#8217;s say you want a result set containing each forum&#8217;s name, and with it the text of the newest [...]]]></description>
			<content:encoded><![CDATA[<p><img style="max-width: 800px; margin:0em 1em 1em 0em;" src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/11/logo-mysql-sun.gif" align="left" />This one was quite a sticky wicket. So what if you have two tables in mySQL, with a one-to-many relationship (say a forum and comments in that forum), and each of the comments is dated. Now let&#8217;s say you want a result set containing each forum&#8217;s name, and with it the text of the newest comment in that forum.</p>
<p>The obvious way to do this is with a subselect, which mySQL doesn&#8217;t have. So how does one do it?</p>
<p>At first I thought that it might be impossible, but I have figured out the answer:</p>
<p><font face="Courier New">SELECT f.forum_id, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max(c.created_on) as last_date, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.created_on as created_on, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2.text <br />FROM&nbsp;&nbsp; forum f <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LEFT JOIN comment c ON (c.forum_id = f.forum_id) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LEFT JOIN comment c2 ON (f.forum_id = c2.forum_id)&nbsp; <br />GROUP BY f.forum_id, c2.comment_id <br />HAVING created_on = last_date OR last_date IS NULL;</font></p>
<p>See, it&#8217;s pretty clever actually, though it leans on the &#8220;HAVING&#8221; option, which is sort of crappy. The trick is to join the comment table with the forum table twice. One of those joins gets grouped by forum id so that you can use the max function on it. The other <b>doesn&#8217;t</b> get grouped (by adding its primary key to the group by clause) so that you can still pull data out of it. Then you use the having clause to find only the row that has the max date.</p>
<p>I am also accepting values with a max value of NULL so that if a forum has no comments, it still comes back in the results.</p>
<p>Hope this helps someone, somewhere.<br />Happy Hacking</p>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2008/11/selecting-the-newest-row-in-one-to-many-on-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Weird little MySQL error.</title>
		<link>http://osric.com/chris/accidental-developer/2008/10/weird-little-mysql-error/</link>
		<comments>http://osric.com/chris/accidental-developer/2008/10/weird-little-mysql-error/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 18:46:00 +0000</pubDate>
		<dc:creator>giblfiz</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=88</guid>
		<description><![CDATA[So I just moved some code onto a new server, and I&#8217;m suddenly getting the warning: mysql_query(): 14 is not a valid MySQL-Link resource in &#60;bla bla bal&#62; on line 47 A few other people seem to have gotten this error, but no one has posted a solution. (though one guy oh-so-annoyingly posted &#8220;I figured [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/10/logo_mysql_sun.gif"><img class="alignright size-medium wp-image-92" title="logo_mysql_sun" src="http://osric.com/chris/accidental-developer/wp-content/uploads/2008/10/logo_mysql_sun.gif" alt="" width="205" height="68" /></a>So I just moved some code onto a new server, and I&#8217;m suddenly getting the warning:</p>
<p>mysql_query(): 14 is not a valid MySQL-Link resource in &lt;bla bla bal&gt; on line 47</p>
<p>A few other people seem to have gotten this error, but no one has posted a solution. (though one guy oh-so-annoyingly posted &#8220;I figured it out, so never mind&#8221; &#8230; Grrr. I mean, if you&#8217;re going to post a question, the answer should be in that thread if you ever figure it out&#8230;.</p>
<p>So as soon as I figure out the answer I&#8217;m going to post it here.</p>
<p><span style="color: #800000;"><strong>I&#8217;m back with the solution:</strong></span></p>
<p><span style="color: #800000;">I got clued onto it from this page: http://bytes.com/forum/thread638479.html . The error is coming because mysql_close was being called by the destructor, and because I was in safe mode the same MySQL resource was being used for each instance. What threw me even more though was that the destructor was being called at all, because I thought I only _had_ one instance. Turns out that there is a spot in my code where I (accidentaly) passed my DB object by value rather than refrence. This made a new copy of the object, which ran mysql_connect again, because it was in safe mode it returened the _same_ refrence. Then the object got unloaded, the destructor ran and closed the refrence, even though there was another instance of the object out there still using the same refrence. </span></p>
<p><span style="color: #800000;">Icky!</span></p>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2008/10/weird-little-mysql-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The most Interesting Code I Have Ever Written</title>
		<link>http://osric.com/chris/accidental-developer/2008/03/the-most-interesting-code-i-have-ever-written/</link>
		<comments>http://osric.com/chris/accidental-developer/2008/03/the-most-interesting-code-i-have-ever-written/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 17:16:27 +0000</pubDate>
		<dc:creator>giblfiz</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[idbg]]></category>
		<category><![CDATA[Introspective Debugger]]></category>
		<category><![CDATA[Live Servers]]></category>

		<guid isPermaLink="false">http://osric.com/chris/accidental-developer/?p=14</guid>
		<description><![CDATA[I wrote this about a month back, and Honestly blew my own mind with it. Its far from the best code I have ever written, something which is further exacerbated by the fact that I was shooting to keep it short and small, which never makes your code good, I was also aiming not to [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote this about a month back, and Honestly blew my own mind with it. Its far from the best code I have ever written, something which is further exacerbated by the fact that I was shooting to keep it short and small, which never makes your code good, I was also aiming not to use anything that is not a core function in PHP. The flip side is that I used a lot of really uncommon php calls that do some very, very cool things. At one point this was an ajax app, but I discovered that it could be smaller and cleaner by just using frames.</p>
<p>This will be the first post in a series where I go through this code and explain it, and some of the cooler calls that it is using, as well as some advanced php and programming concepts. But for now I&#8217;m just going to give a one paragraph overview of what the code does and get it into the post. There is one other file which I will also post and explain at some point.</p>
<p>So read On to take a look at the nitty-gritty, and get a feel for what it does in total<br />
<span id="more-14"></span></p>
<p>I call this program the introspective debugger. The basic Idea is that you include this file as the first line of any script you are running, and then when you visit the page, rather than getting a normal page, you end up with an interactive debugger. This is done with frames. In one frame you can look at all of the variables, or at the source code with your current line highlighted or the stack trace, In another you can look at the output so far. Sadly at this time you can&#8217;t change variables on the fly. You can then tell the program to run again until it hits the next break-point, or until it ends.</p>
<p>Here is the full source code for introspective debugger:</p>
<pre>
<code>&amp;lt;?php
$idbg_var_exclude_list = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'idbg_var_exclude_list' , 'idbg_stop_flag', 'idbg_var_spy', 'idbg_backtrace');
$old_error_level = error_reporting(0);
if(array_key_exists("idbg_id",$_GET)){
  $idbg_id = $_GET["idbg_id"];
} else {
  $idbg_id = 1025 + rand(5000,8000); // get a random high port
}
$callBackName= htmlspecialchars_decode($_SERVER['SCRIPT_NAME']."?idbg_id=$idbg_id&amp;idbg_mode=");
$callBackWvars= htmlspecialchars_decode($_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],"?")?"&amp;":"?")."idbg_id=$idbg_id&amp;idbg_mode=");
if(!array_key_exists("idbg_mode",$_GET)){
  /* Display Frame Mode */
  echo("&amp;lt;html&amp;gt;
 &amp;lt;frameset rows='65%,35%'&amp;gt;
   &amp;lt;frame src='$callBackName"."cnc' name='infoframe'&amp;gt;
   &amp;lt;frame src='$callBackWvars"."run' name='scriptframe'&amp;gt;
&amp;lt;/frameset&amp;gt;
&amp;lt;/html&amp;gt;");
  exit();

} else if (array_key_exists("idbg_mode",$_GET) and ($_GET["idbg_mode"] == "cnc" or $_GET["idbg_mode"] == "fstep")){
  if(array_key_exists("idbg_act",$_GET)){ idbg_socket_client($_GET["idbg_act"]);}
  $responce = unserialize(idbg_socket_client("cnc"));
  if($_GET["idbg_mode"] == "fstep"){ header( "Location: ".$callBackName."file&amp;idbg_open_line=".$responce['trace'][0]['line']. "&amp;idbg_open=".$responce['trace'][0]['file']."#anchor");}
  echo ("&amp;lt;h1&amp;gt; I am Command &amp; Control DIV $idbg_id &amp;lt;/h1&amp;gt;");
  echo("[&amp;lt;a href='".$callBackName."cnc' target='infoframe'&amp;gt;CNC&amp;lt;/a&amp;gt;] ");
  echo("[&amp;lt;a href='".$callBackName."cnc&amp;idbg_act=step' target='infoframe'&amp;gt;STEP&amp;lt;/a&amp;gt;] ");
  echo("[&amp;lt;a href='".$callBackName."cnc&amp;idbg_act=cont' target='infoframe'&amp;gt;CONTINUE&amp;lt;/a&amp;gt;]&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;");
  foreach($responce["trace"] as $key =&amp;gt; $val){
    echo("&amp;lt;a href='".$callBackName."file&amp;idbg_open_line=".$val['line'].
	 "&amp;idbg_open=".$val['file']."#anchor' target='infoframe'&amp;gt;".strrchr($val['file'],"/")."&amp;lt;/a&amp;gt;");
    echo(" Line:". $val['line'] . " Function:" . $responce["trace"][$key+1]["function"]. "
  &amp;lt;a href='#' onClick="document.getElementById('v_$key').style.display='block'"&amp;gt;+&amp;lt;/a&amp;gt;
          &amp;lt;div id='v_$key' style='display:none'&amp;gt;&amp;lt;pre&amp;gt;");
    print_r($responce["vars"][$responce["trace"][$key+1]["function"]]);
    echo("&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;hr/&amp;gt;");
  }
  exit();

} else if (array_key_exists("idbg_mode",$_GET) &amp;&amp; $_GET["idbg_mode"] == "file"){
  echo("[&amp;lt;a href='".$callBackName."cnc' target='infoframe'&amp;gt;CNC&amp;lt;/a&amp;gt;] ");
  echo("[&amp;lt;a href='".$callBackName."fstep&amp;idbg_act=step' target='infoframe'&amp;gt;STEP&amp;lt;/a&amp;gt;] ");
  echo("[&amp;lt;a href='".$callBackName."fstep&amp;idbg_act=cont' target='infoframe'&amp;gt;CONTINUE&amp;lt;/a&amp;gt;]&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;
        &amp;lt;div style='background-color:#CCC'&amp;gt;");
   $fp = fopen($_GET["idbg_open"],"r");
   $current_line = 0;
   while(!feof($fp)){
    $current_line++;
    if($current_line &amp;lt; $_GET["idbg_open_line"]){
      $top .= (fgets($fp));
    }else if($current_line == $_GET["idbg_open_line"]){
      $keyline.= (fgets($fp));
    } else {
      $bottom .= (fgets($fp));
    }
   }

   echo(str_replace("a7R4449p","&amp;lt;a name='anchor'/&amp;gt;
        &amp;lt;div style='background-color:#BBF;border: thin solid #000'&amp;gt;",str_replace("b7R4449p","&amp;lt;/div&amp;gt;",
	highlight_string($top . "a7R4449p". $keyline ."b7R4449p" . $bottom, TRUE))));
   echo("&amp;lt;/div&amp;gt;");
  fclose($fp);
  exit();

} else if (array_key_exists("idbg_mode",$_GET) &amp;&amp; $_GET["idbg_mode"] == "run"){
  echo ("&amp;lt;h1&amp;gt; I am running DIV $idbg_id &amp;lt;/h1&amp;gt;");
  register_tick_function("idbg_tick_function");
}

function idbg_socket_client($msg){
  global $idbg_id;
  //echo ("=&amp;gt;".$msg."&amp;lt;BR/&amp;gt;");flush();
  do {
    usleep(rand(50000,150000));
    $conn = stream_socket_client("tcp://".$_SERVER["REMOTE_ADDR"].":$idbg_id", $errno, $errstr);
    //echo($errstr . "&amp;lt;= ");flush();
  } while($errno == 111);

  if (!$conn) { echo "Debugger couldn't visit socket: $errstr ($errno)&amp;lt;br /&amp;gt;\n"; } else {
    fwrite($conn, $msg);
    while (!feof($conn)) {
      $responce .= fgets($conn, 1024);
      //echo ("-&amp;gt;".$responce);flush();
    }
    fclose($conn);
    return($responce);
  }
}

function idbg_tick_function(){
  global $idbg_stop_flag, $idbg_id, $idbg_var_spy, $idbg_last_line, $idbg_backtrace;
  $idbg_backtrace = debug_backtrace();
  //  echo ($idbg_last_line. " ?= " .$idbg_backtrace[0]['line'] ." in ".$idbg_backtrace[0]['file']."&amp;lt;Br/&amp;gt;" );
  if($idbg_stop_flag and $idbg_last_line != $idbg_backtrace[0]['line'] and !strpos($idbg_backtrace[0]['file'], 'STOP.php')){
    //echo("&amp;lt;/hr&amp;gt; idbg_tick_function START &amp;lt;br/&amp;gt;");
    $socket = stream_socket_server("tcp://".$_SERVER["REMOTE_ADDR"].":$idbg_id", $errno, $errstr);
    if (!$socket) { echo "&amp;lt;h1&amp;gt;Debugger couldn't open socket: $errstr ($errno)&amp;lt;h1/&amp;gt;\n";} else {
      while ($conn = stream_socket_accept($socket)) {
	$msgIn = fread($conn, 1024);
	echo($msgIn);
	flush();
	if ($msgIn == 'cnc'){
	  fwrite($conn, serialize(array("trace"=&amp;gt;$idbg_backtrace,"vars" =&amp;gt; $idbg_var_spy)));
	} else if ($msgIn == 'step'){
	  fclose($conn);
	  fclose($socket);
	  $idbg_last_line = $idbg_backtrace[0]['line'];
	  return(TRUE);
	} else if ($msgIn == 'cont'){
	  //	  fwrite($conn, "ACK");
	  fclose($conn);
	  fclose($socket);
	  $idbg_last_line = 0;
	  $idbg_stop_flag = FALSE;
	  return(TRUE);
	} else {
	  fwrite($conn, " This is a SERVER Responce \n");
	}
	fclose($conn);
      }
      fclose($socket);
    }
  }
}//else just keep going
error_reporting($old_error_level);

?&amp;gt;</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://osric.com/chris/accidental-developer/2008/03/the-most-interesting-code-i-have-ever-written/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

