<?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>Some Guy Ranting &#187; MySQL</title>
	<atom:link href="http://lukewelling.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://lukewelling.com</link>
	<description>Just another nerd’s weblog</description>
	<lastBuildDate>Thu, 17 Dec 2009 23:24:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OSCON 2007 Tutorial: PHP and MySQL Best Practices</title>
		<link>http://lukewelling.com/2007/07/25/oscon-2007-tutorial-php-and-mysql-best-practices/</link>
		<comments>http://lukewelling.com/2007/07/25/oscon-2007-tutorial-php-and-mysql-best-practices/#comments</comments>
		<pubDate>Tue, 24 Jul 2007 14:56:19 +0000</pubDate>
		<dc:creator>luke</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lukewelling.com/2007/07/25/oscon-2007-tutorial-php-and-mysql-best-practices/</guid>
		<description><![CDATA[Here are the slides for our talk today.
best_practices.pdf
If this site is slow, you can try http://www.laurathomson.com
]]></description>
			<content:encoded><![CDATA[<p>Here are the slides for our talk today.</p>
<p><a id="p113" href="http://qs2398.pair.com/luke/lukewelling/wordpress/wp-content/uploads/2007/07/best_practices.pdf">best_practices.pdf</a></p>
<p>If this site is slow, you can try <a href="http://www.laurathomson.com">http://www.laurathomson.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lukewelling.com/2007/07/25/oscon-2007-tutorial-php-and-mysql-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with Alexadex</title>
		<link>http://lukewelling.com/2006/02/27/fun-with-alexadex/</link>
		<comments>http://lukewelling.com/2006/02/27/fun-with-alexadex/#comments</comments>
		<pubDate>Mon, 27 Feb 2006 12:16:05 +0000</pubDate>
		<dc:creator>luke</dc:creator>
				<category><![CDATA[Alexadex]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[hacking]]></category>

		<guid isPermaLink="false">http://lukewelling.com/2006/02/27/fun-with-alexadex/</guid>
		<description><![CDATA[In case you are not aware, Alexadex is a virtual stock market game, where the values of stocks depend on their Alexa reach ratings.
Because I have too much time on my hands, I wanted to track my portfolio value in the sidebar of my blog.  Look over there somewhere &#8212;&#8211;&#62; and you will probably [...]]]></description>
			<content:encoded><![CDATA[<p>In case you are not aware, <a href="http://alexadex.com/ad/index.fcgi?ref=7302">Alexadex</a> is a virtual stock market game, where the values of stocks depend on their <a href="http://alexa.com">Alexa</a> reach ratings.</p>
<p>Because I have too much time on my hands, I wanted to track my portfolio value in the sidebar of my blog.  Look over there somewhere &#8212;&#8211;&gt; and you will probably see it.</p>
<p>In case it holds amusement value to somebody, here is the code. It relies on PHP and MySQL and just does some simple screen scraping.</p>
<p>The fact that this URL works:<br />
<a href = "http://alexadex.com/ad/api?&#038;method=getQuote&#038;url=lukewelling.com">http://alexadex.com/ad/api?&#038;method=getQuote&#038;url=lukewelling.com</a><br />
hints that there might be an API to do this at some point, but for now, I am screen scraping. (url pulled from <a href = "http://www.calevans.com/view.php/page/alexadex">Cal Evans&#8217; blog</a>)</p>
<p>The database table looks like this:</p>
<p><code>
<pre>
CREATE TABLE alexadex (
  timestamp timestamp(14) NOT NULL,
  value int(11) NOT NULL default '0',
  PRIMARY KEY  (timestamp)
)
</pre>
<p></code></p>
<p>From a cron job I am running:<br />
<code>
<pre>
&lt;?php
require('functions.php');

connectToDb();

$username = 'tangledweb';
$url = "http://alexadex.com/ad/user/$username";
$marker = 'total:&lt;/b&gt;&lt;/td&gt;&lt;td align=right&gt;$';

$current =  scrape( $url, $marker );
if($current!==false)
{
   echo "stored: ";
   storeCurrent($current);
}

echo $current;

?&gt;
</pre>
<p></code><br />
In case it is not obvious, my Alexadex username is tangledweb.</p>
<p>In my blog sidebar I have:<br />
<code>
<pre>
&lt;?php
require('functions.php');
echo '&lt;li&gt;&lt;a href = "http://alexadex.com/ad/user/tangledweb"
      &gt;My current portfolio is $';
$temp = getMostRecentFromDb();
echo number_format($temp['value']).'&lt;/a&gt;';
?&gt;
</pre>
<p></code></p>
<p>The functions these rely on are:<br />
<code>
<pre>
function storeCurrent($value)
{
 $value = intval($value);
 $sql = "INSERT
         INTO alexadex
         VALUES (NOW(), $value)";
  $result = mysql_query($sql);
}

function getMostRecentFromDb()
{
  $sql = "SELECT *
          FROM alexadex
          WHERE 1
          ORDER BY `timestamp` DESC
          LIMIT 1";

  $result = mysql_query($sql);

  return mysql_fetch_array($result);
}

function scrape($url, $marker, $maxLength = 50)
{
  $page = file_get_contents($url);
  if($page === false)
  {
    return false;
  }
  $pos = strpos($page, $marker);
  if($pos === false)
  {
    return false;
  }
  $value= substr($page, $pos + strlen($marker), $maxLength);
  $value= str_replace(',', '', $value);
  $value= intval($value);
  return $value;
}

function connectToDb()
{
  $connection = mysql_connect("host",
                              "user",
                              "pass");
  mysql_select_db("dbname", $connection);
}
</pre>
<p></code></p>
<p>This code comes with no warranty of any kind.  You can have it as public domain, but I would appreciate a link to this blog if you use it.  I hope it still works.  WordPress seems to really, really want to mess with it when it saves it.</p>
]]></content:encoded>
			<wfw:commentRss>http://lukewelling.com/2006/02/27/fun-with-alexadex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

