<?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>Alessio Sclocco &#187; MySQL</title>
	<atom:link href="http://alessio.sclocco.eu/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://alessio.sclocco.eu</link>
	<description>who I am and what I&#039;m doing</description>
	<lastBuildDate>Sun, 11 Dec 2011 17:59:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL class 0.2</title>
		<link>http://alessio.sclocco.eu/mysql-class-02/</link>
		<comments>http://alessio.sclocco.eu/mysql-class-02/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 18:50:27 +0000</pubDate>
		<dc:creator>Alessio Sclocco</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://alessio.sclocco.eu/?p=57</guid>
		<description><![CDATA[During March I wrote a post about a simple class that I use when I need to communicate with a MySQL database in PHP 5. Now it&#8217;s time to release version 0.2 of that class. There are two changes from the past version: some bugs fixed; added a new method named &#8220;count&#8220;. The new method [...]]]></description>
			<content:encoded><![CDATA[<p>During March I wrote a post about <a title="MySQL class 0.1" href="http://alessio.sclocco.eu/mysql-class-01/">a simple class that I use when I need to communicate with a MySQL database in PHP 5</a>.<br />
Now it&#8217;s time to release version 0.2 of that class.</p>
<p>There are two changes from the past version:</p>
<ol>
<li>some bugs fixed;</li>
<li>added a new method named &#8220;<em>count</em>&#8220;.</li>
</ol>
<p>The new method is designed to count the occurrences of &#8220;something&#8221; &#8220;somewhere&#8221; so when you need to do something like:</p>
<pre class="brush: sql; title: ; notranslate">SELECT COUNT(some columns) FROM some tables WHERE some conditions;</pre>
<p>you can simply type (assuming you have created a $database object before):</p>
<p><code><br />
</code></p>
<p><code>
<pre class="brush: php; title: ; notranslate">$database-&gt;count(&quot;some columns&quot;, &quot;some tables&quot;, &quot;some conditions&quot;);</pre>
<p></code></p>
<p>Enjoy!</p>
<p><a href="http://alessio.sclocco.eu/wp-content/uploads/2009/02/mysql_classphp.bz2">Download the MySQLDB class source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alessio.sclocco.eu/mysql-class-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL class 0.1</title>
		<link>http://alessio.sclocco.eu/mysql-class-01/</link>
		<comments>http://alessio.sclocco.eu/mysql-class-01/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 18:40:47 +0000</pubDate>
		<dc:creator>Alessio Sclocco</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://alessio.sclocco.eu/?p=53</guid>
		<description><![CDATA[Some times ago I wrote that simple class in PHP (keep in mind: it&#8217;s not compatible with PHP4) for talking with a MySQL database and now that I&#8217;ve used it a little bit in some projects i want to share the code with all of you. First of all let me say that probably you [...]]]></description>
			<content:encoded><![CDATA[<p>Some times ago I wrote that simple class in PHP (keep in mind: it&#8217;s not compatible with PHP4) for talking with a MySQL database and now that I&#8217;ve used it a little bit in some projects i want to share the code with all of you.</p>
<p>First of all let me say that probably you can use for all life the classical <a href="http://www.php.net/manual/en/ref.mysql.php">mysql functions</a> and not suffer of any &#8220;damage&#8221; and obviously I use those functions inside my own class, but if you want to write code in a more &#8220;<strong>OO way</strong>&#8221; and a more readable code too, you may give a try to that class.<br />
Oh, don&#8217;t worry, I don&#8217;t go mad if you don&#8217;t want or don&#8217;t like my class.</p>
<p>So, let&#8217;s stop with that insane writing and let me introduce briefly how class work in a user perspective, you can read all the source code until you&#8217;ve downloaded the source file.</p>
<p>First of all we need to construct an object of type MySQLDB:</p>
<pre class="brush: php; title: ; notranslate">$database = new MySQLDB();</pre>
<p>There are three possible parameters you can pass to the constructor: <strong>username</strong>, <strong>password</strong> and <strong>hostname</strong> for accessing the server; those parameters are respectively, in case you leave them empty, an empty string for the first and second and &#8220;localhost&#8221; for the third.<br />
You can easily change that parameters later using methods like <code>setUser()</code>, <code>setPassword()</code> or <code>setServer()</code> but I think it&#8217;s easier to pass first and second to the constructor instead:</p>
<pre class="brush: php; title: ; notranslate">$database = new MySQLDB(&quot;username&quot;, &quot;passoword&quot;);</pre>
<p>Obviously I&#8217;m talking in the eventuality that your DBMS is located on localhost.<br />
If username, password and hostname are correctly given to the object you can connect to the database:</p>
<p>$database-&gt;connect();</p>
<p>You can at any time change database doing something like that:</p>
<pre class="brush: php; title: ; notranslate">$database-&gt;disconnect();
$database-&gt;setUser(&quot;newuser&quot;);
$database-&gt;setPassword(&quot;newpassword&quot;);
$database-&gt;setServer(&quot;newserver&quot;);
$database-&gt;connect();
</pre>
<p>Yeah, maybe you can write few rows of code doing by yourself, but if you don&#8217;t need to change all parameters it can be cool.<br />
When you&#8217;re connected you can access a database in that way:</p>
<pre class="brush: php; title: ; notranslate">$database-&gt;setDatabase(&quot;database&quot;);</pre>
<p>For setting and executing a query:</p>
<pre class="brush: php; title: ; notranslate">$database-&gt;setQuery(&quot;QUERY ...&quot;);
$database-&gt;query();</pre>
<p>Maybe i had had to advert you that some of this methods can throw exceptions, so, for your safety is better if you use <strong>try &amp; catch</strong> blocks that my laziness prevents me to write right now in that examples :)<br />
If you want to retrieve data from a select type of query or maybe know how much rows you deleted/updated and things like that you can use that powerful duo: <code>getResultsNumber()</code> and <code>getData()</code>.<br />
A couple of examples can explain how them work:</p>
<pre class="brush: php; title: ; notranslate">
//  I've done an insert before and want to know if all was well

if ( $database-&gt;getResultsNumber() == 0 ) {
print(&quot;Something bad happened!&quot;);
}
// I've done a select between multiple rows and now want to use the data collected

for ( $i = 0; $i &lt; $database-&gt;getResultsNumber(); $i++ ) {
$temp = $database-&gt;getData();
/* do something with the object $temp that has an attribute for every column you put in the select */
}</pre>
<p>No need to call a special method for closing connection or cleaning the state, the destructor makes all the work for you!<br />
What can I say more ?<br />
Maybe you can write yourself or download a better MySQL class, but as I said, I&#8217;ve wrote that class and I prefer to share code than lost it on a remote directory of my computer.<br />
Last thing to say is that MySQLDB class is released under <a href="http://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a> license and that you can write me, or contact me in the way you prefer, for every question/comment.</p>
<p><a href="http://alessio.sclocco.eu/wp-content/uploads/2009/02/mysql_classphp.bz2">Download the MySQLDB class source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alessio.sclocco.eu/mysql-class-01/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

