<?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>Doing My Best... a Web Developers Blog &#187; MySQL</title>
	<atom:link href="http://www.iwilldomybest.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iwilldomybest.com</link>
	<description>Ranging from PHP &#38; MySQL to Random Crap from the internet, I got it all here.</description>
	<lastBuildDate>Wed, 10 Nov 2010 01:08:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>PHP &amp; MySQL Tip #3 &#8211; EAV Modeling w/ PHP &amp; MySQL</title>
		<link>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/</link>
		<comments>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 22:52:05 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[eav]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/2008/08/25/php-mysql-tip-3/</guid>
		<description><![CDATA[PHP &#38; MySQL Tip #3 I want to share with some of you an easy way to set up your database design in a very flexible and reliable EAV like model. EAV stands for Entity Attribute Value, which is a common design for complex database structures that require many different Entities using many different attributes [...]]]></description>
			<content:encoded><![CDATA[<p>PHP &amp; MySQL Tip #3</p>
<p>I want to share with some of you an easy way to set up your database design in a very flexible and reliable EAV like model.</p>
<p><span id="more-20"></span></p>
<p><a title="http://en.wikipedia.org/wiki/Entity-attribute-value_model" href="http://en.wikipedia.org/wiki/Entity-attribute-value_model" target="_blank">EAV</a> stands for Entity Attribute Value, which is a common design for complex database structures that require many different Entities using many different attributes with, again, many different values. It's very common to find this set up in medical offices or records.</p>
<p>Let's go ahead and assume a situation, and we'll work through it.</p>
<p>Let's say you're accepting a feed of some sort, and you need to save the items being fed to you into a database. The problem here, is that the content length of these items, attributes, and values varies.</p>
<p>So let's consider the following:</p>
<pre>Item 'Node1' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
Item 'Node2' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
	Attribute 'Other' -&gt;
		Value 1
		Value 2

Item 'Node3' -&gt;
	Attribute 'Main' -&gt;
		Value 1
		Value 2
		Value 3
		Value 4
		Value 5
		Value 6
		Value 7
		Value 8</pre>
<p>Now this is a very cheap and generic example of a data structure. But we'll work with it for now.</p>
<p>So let's go ahead and draw some conclusions.</p>
<p>The Item list will come in with a name, and have Attributes. These attributes will have values. But the number of attributes and the number of values varies.</p>
<p>So with that in mind, we shall come up with some SQL to create some times. For this example we're going to need three tables: items, item_attributes, and attribute_values.</p>
<p>So here we go:</p>
<pre class="brush:sql">
-- first our items table:
CREATE TABLE `items` (
`id` int(11) NOT NULL auto_increment,
`item_name` varchar(50) default NULL,
PRIMARY KEY  (`id`)
);

-- now our item attributes
CREATE TABLE  `item_attributes` (
`id` int(11) NOT NULL auto_increment,
`item_id` int(11) NOT NULL default '0',
`attribute_name` varchar(50) default NULL,
PRIMARY KEY  (`id`),
KEY `item_id_attribute_name` (`item_id`,`attribute_name`)
);

-- now finally our attribute values
CREATE TABLE  `attribute_values` (
`attribute_id` int(11) NOT NULL default '0',
`attribute_value` varchar(100) default NULL,
UNIQUE KEY `attribute_id` (`attribute_id`,`attribute_value`)
);
</pre>
<p>Now that we have our layout, let's take a look at how this is going to work:</p>
<p>Let's assume the following PHP array is a reprentation of our data...</p>
<pre class="brush:php">
&lt;?php

// start up our array
$data = array();
//
// Now, let's just load it with some test data
$data['item_1'] = array();
$data['item_1']['attribute_1'] = array();
$data['item_1']['attribute_1'][] = 'value1';
$data['item_1']['attribute_1'][] = 'value2';
$data['item_1']['attribute_1'][] = 'value3';
$data['item_1']['attribute_1'][] = 'value4';
// that's good for now.

/**
* Now let's insert this int our new schema
*
* Please note, for example sake, I will not be double checking queries
* but you SHOULD check each query for an error.
**/

foreach($data as $item_name =&gt; $attributes)
{
	$sql = "INSERT INTO items (id, item_name) VALUES (NULL, '{$item_name}');";
	mysql_query($sql);
	$item_id = mysql_insert_id();
	// now let's loop through our attributes
	foreach($attributes as $attribute =&gt; $values)
	{
		// this is now our insert into the attributes...
		$sql = "INSERT INTO item_attributes (id, item_id, attribute_name) VALUES (NULL, {$item_id}, '{$attribute}');";
		mysql_query($sql);
		$attribute_id = mysql_insert_id();
		// now let's loop through the attribute values
		foreach($values as $value)
		{
			$sql = "INSERT INTO attribute_values (attribute_id, attribute_value) VALUES ({$attribute_id}, '{$value}');";
			mysql_query($sql);
		}
	}
}
?&gt;
</pre>
<p>And there you have it - that's now how we can get our data into our database in a flexiable manner, without having to rely<br />
on an 'excel' like database.</p>
<p>To get it out, we'll simply use some joins...</p>
<pre class="brush:php">
$sql =
	"SELECT
			items.item_name,
			ia.attribute_name,
			av.attribute_value
	 FROM
			attribute_values AS av
		JOIN item_attributes AS ia
			ON (ia.id = av.attribute_id)
		JOIN items AS items
			ON (items.id = ia.item_id);
	";
</pre>
<p>You could also use a concat_ws to make a comma seperated list, but now that you have it in your database, you can do anything you want with it <img src='http://www.iwilldomybest.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/08/php-mysql-tip-3/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>PHP &amp; MySQL Tip #2 &#8211; Better Way of Pagination</title>
		<link>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/</link>
		<comments>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 04:30:27 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.iwilldomybest.com/2008/03/15/php-mysql-tip-better-way-of-pagination/</guid>
		<description><![CDATA[I've seen this come up a few times in #php on irc.gamesurge.net - how to find the total number of rows found, while using the LIMIT clause on a query. I've seen people respond with some seriously incorrect solutions, such as 'Simply load all of the results into an array and only show X amount' [...]]]></description>
			<content:encoded><![CDATA[<p>I've seen this come up a few times in #php on irc.gamesurge.net - how to find the total number of rows found, while using the LIMIT clause on a query.</p>
<p>I've seen people respond with some seriously incorrect solutions, such as 'Simply load all of the results into an array and only show X amount' or 'Simple run another query without the LIMIT clause and count those rows.'</p>
<p>Those two, common, solutions are sadly not the best solution.</p>
<p>Read on to discover my solution.</p>
<p><span id="more-16"></span></p>
<p>Take for example the following query:</p>
<p><code>$sql = "SELECT id as id, name as name, full_title as title FROM articles ORDER BY id LIMIT 0,10";</code></p>
<p>Now, that looks all fine and dandy, you have probably used something very similar to that query many times. Now, say we want to actually show how many articles we have in our database?</p>
<p>We would simply change that query to:</p>
<p><code><br />
$sql = "SELECT SQL_CALC_FOUND_ROWS id as id, name as name, full_title as title FROM articles ORDER BY id LIMIT 0,10";<br />
$get = mysql_query($sql) or die(mysql_error());<br />
$sql_2 = "SELECT FOUND_ROWS();";<br />
$found_rows = mysql_query($sql_2) or die(mysql_error());<br />
</code></p>
<p>Now yes, we're running two queries, but we're not actually queries against any rows. So therefore, this solution is more elegant and ultimately more important, it's not attempting to scan any tables for data.</p>
<p>So next time you're looking to do a quick and pagination script, go ahead and try this technique out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2008/03/php-mysql-tip-2-better-way-of-pagination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP and MySQL Tip #1 &#8211; Error Checks</title>
		<link>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/</link>
		<comments>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/#comments</comments>
		<pubDate>Tue, 01 May 2007 01:38:26 +0000</pubDate>
		<dc:creator>dschreck</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.e-dirtylaundry.com/2007/09/28/php-and-mysql-tip-1-error-checks/</guid>
		<description><![CDATA[I'd like to share with all of you some PHP Tips. Use error checking when making an SQL query. All too often I see on IRC &#38; Forums people complaining/asking for help, regarding their mysql_query(). "It was working fine yesterday, now it''s broken." "Can someone tell me why this doesn''t work?" "Why doesn''t this work? [...]]]></description>
			<content:encoded><![CDATA[<p>I'd like to share with all of you some PHP Tips.<br />
<span id="more-3"></span><br />
Use error checking when making an SQL query.</p>
<p>All too often I see on IRC &amp; Forums people complaining/asking for help, regarding their mysql_query(). "It was working fine yesterday, now it''s broken." "Can someone tell me why this doesn''t work?" "Why doesn''t this work? It just doesn''t do anything."</p>
<p>People, you <strong>need</strong> to check for errors when you run a query. This is generally done like so:</p>
<p><code><br />
$get = mysql_query($sql) or die (mysql_error());<br />
</code></p>
<p>Now, that in itself is rather simple to understand. If there are any error's with your query ($sql), your script will die() and tell you the error.</p>
<p>An even better implementation of this would be:</p>
<p><code><br />
if(!$get = mysql_query($sql)) {<br />
die("Get query evaluated to false. SQL : {$sql} MySQL Error: ".mysql_error());<br />
}<br />
</code></p>
<p>Now, this is great for debugging purposes. It won't really fly on a live front end site. So you'd swap out the die() with:</p>
<p><code><br />
if(!$get = mysql_query($sql)) {<br />
error_log("Get query evaulted to false. SQL : {$sql} - MySQL Error: ".mysql_error());<br />
} else {<br />
/* Continue on with logic that relies upon this query */<br />
}<br />
</code></p>
<p>Now, what happens is if your query failed, you will get an error_log message, and you <span style="font-style: italic">do</span> check your error logs? Right? Of course! I thought so.<br />
Also, this ensures that only <span style="font-weight: bold">after</span> we know our query was successful do we continue on with our logic that relies upon it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwilldomybest.com/2007/04/php-and-mysql-tip-1-error-checks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

