<?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>9tutorials - The best collection of tutorials &#187; Database</title>
	<atom:link href="http://9tutorials.com/category/database/feed" rel="self" type="application/rss+xml" />
	<link>http://9tutorials.com</link>
	<description>Photoshop tutorials , Flash tutorials, PHP tutorials and much more</description>
	<lastBuildDate>Sat, 20 Feb 2010 20:24:02 +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>Converting MS Access to MySQL</title>
		<link>http://9tutorials.com/2007/09/06/converting-ms-access-to-mysql.html</link>
		<comments>http://9tutorials.com/2007/09/06/converting-ms-access-to-mysql.html#comments</comments>
		<pubDate>Fri, 07 Sep 2007 01:48:36 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/09/06/converting-ms-access-to-mysql.html</guid>
		<description><![CDATA[For small and large database projects, Microsoft Access works wonderfully. If you have a database with hundreds of thousands of records, maybe even connecting to other databases remotely. Security isn&#8217;t too much of a problem (no Access database is truly secure), and with a wide range of options for publishing and reporting on your data.
That [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>For small and large database projects, Microsoft Access works wonderfully. If you have a database with hundreds of thousands of records, maybe even connecting to other databases remotely. Security isn&#8217;t too much of a problem (no Access database is truly secure), and with a wide range of options for publishing and reporting on your data.<span id="more-3125"></span></p>
<p>That is, until you try to put it online. That&#8221;s where converting your Access database to MySQL comes in.</p>
<p>While Microsoft Access is great as a desktop solution, it weakens greatly when transferred online. The limits on users and speed still exist, and it&#8221;s not built to work well on platforms other than Microsoft. Unfortunately for Microsoft Access, most web servers run a form of Unix, not Windows.</p>
<p>Enter MySQL.</p>
<p>MySQL is a relational database system designed specifically for use online and housed on the most common servers online. It&#8221;s fast, robust, clean, free, and fairly intuitive to work with as long as you have a very solid understanding of all things SQL. Converting to MySQL from Microsoft Access isn&#8221;t without it&#8221;s set of problems, however there are a few things you can do to make sure that this conversion goes smoothly.</p>
<p>1. Organize and clean your Microsoft Access database before converting to MySQL! This can&#8221;t be stressed enough and it&#8221;s pretty much the sole reason your conversion from Access to MySQL will fail or succeed. Think of upgrading from Access to MySQL like a complicated high-wire act. If one person has trouble going across that wire, chances are making him ride a bicycle across it while balancing a ball on his nose will only end up in disaster &#8230; and a broken bicycle. Re-design your Microsoft Access database if you have to, but be absolutely sure that your database is designed flawlessly.</p>
<p>2. Set up your MySQL database. Just as making sure your Access database is designed flawlessly, your MySQL database must be designed just as flawlessly. MySQL has different data types than Microsoft Access does &#8211; you won&#8221;t find memo fields, for instance &#8211; so be sure that your existing data will fit into the structure you build for MySQL. Otherwise, all you&#8221;re going to get is a mess of mish-mashed data.</p>
<p>3. Export your Microsoft Access data to text files. Fortunately, MySQL has a very handy interface called phpMyAdmin. This allows you to interact directly with your tables through a somewhat graphical UI. All you need to have are tables and fields set up that match your data, and you can click, &#8220;Import Data from Text File&#8221; through phpMyAdmin. Browse to the text file, import it, and congratulations, your data is online!</p>
<p>While there is certainly more details to be learned regarding the process of converting from Microsoft Access to MySQL, always keep in mind the fundamentals. Databases that are designed perfectly will circumvent 95% of the problems that generally befall poorly designed ones. Converting Access to MySQL isn&#8221;t terribly difficult or time consuming, but it can be very tricky. Back up your data, make sure you&#8221;ve designed databases properly on both sides, and your data could be up in no time. Then, it&#8221;s on to PHP!</p>
<p><em>Author: David Badurina, President of Blue Moose Technology, LLC, is a relational database design expert specializing in turning hideous data nightmares into efficient, intuitive database applications.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/09/06/converting-ms-access-to-mysql.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XML Support in Microsoft SQL SERVER</title>
		<link>http://9tutorials.com/2007/06/20/xml-support-in-microsoft-sql-server.html</link>
		<comments>http://9tutorials.com/2007/06/20/xml-support-in-microsoft-sql-server.html#comments</comments>
		<pubDate>Wed, 20 Jun 2007 18:20:38 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Database]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/06/20/xml-support-in-microsoft-sql-server.html</guid>
		<description><![CDATA[This article explains how to retrieve XML document by using data stored in SQL SERVER.And here iam not doing any hard code, simply by configuring the IIS to support XML to retrieve data from database.
 About XML
In the business world, clients share data to carry out business. The data that client share can be stored [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p><span id="lblSubTitle" class="v11">This article explains how to retrieve XML document by using data stored in SQL SERVER.And here iam not doing any hard code, simply by configuring the IIS to support XML to retrieve data from database.</span><span id="more-1264"></span></p>
<p><strong> About XML</strong></p>
<p>In the business world, clients share data to carry out business. The data that client share can be stored in different formats. To integrate and exchange the data stored in different data formats, you need to convert the data into one common format for exchange between different applications.</p>
<p>To overcome this, W3C defines XML as the common data format for exchange between different applications. And also XML is CrossPlatform, hardware and software independent. XML is purely Text Based Mark Up Language; here we concentrate on â€œWhat the DATA â€œnot â€œHow the DATAâ€?. It enables you to store data in a structured format by using meaningful tags. XML is very similar to database. Unlike database, XML document store data in the form of plain text, which can be used across various platform.</p>
<p>In XML, you can very well specify the structure of the data by creating a Document Type Definition (DTD) or XML schema. When you include DTD in the XML Document, the software checks the structure of the XML Document against the DTD. The process of checking the structure of the XML Document is called validating. And this carry out by PARSER In DTD, we can very well specify the Structure and what comes next and also the data type in DTD.</p>
<p><strong>Sample XML Document</strong></p>
<p>[source:xml]</p>
<p>&lt;? xml version=â€?1.0â€?? &gt;<br />
&lt;stores&gt;<br />
&lt;store id=â€?1001â€? &gt;<br />
&lt;title&gt;BU1032 &lt;/title&gt;<br />
&lt;price&gt;450 &lt;/price&gt;<br />
&lt;ord_date&gt;29-062006 &lt;/ord_date&gt;<br />
&lt;/store&gt;<br />
&lt;store id=â€?1002â€? &gt;<br />
&lt;title&gt;AU1032 &lt;/title&gt;<br />
&lt; price&gt;550 &lt;/price&gt;<br />
&lt; ord_date&gt;13-052006 &lt;/ord_date&gt;<br />
&lt;/store&gt;<br />
&lt;/stores&gt;</p>
<p>[/source]</p>
<p>The above XML Document does not contain any formatting instruction. To change the look and feel of the XML Document, W3C has specified extensible stylesheet language transformation (XSLT). XSLT is the language used to format the XML Document and displaying them in required format. XSLT follows the XML syntax.</p>
<p><strong>Retrieving XML Document By Using Data Stored In SQL SERVER</strong></p>
<p>Most of them are familiar with, How webpage is being hosted in the web server and how the data getting retrieved from the database and how the data getting updated in the database. However, if you want to display the data stored on the database in web page, you need to write the code by using any technology like ASP or ADO. In order to simplify the task of creating web pages that display data from Database, Ms-SQL Server2000 provides integration with XML. SQL Server 2000 allows you to generate XML Document by using data stored in a DB. These document can be used for providing devices-independent data. You can also display the resulting XML Document in the supporting web browser.</p>
<p><strong>Support feature in SQL Server 2000</strong></p>
<p>Sql server is a RDBMS. Therefore all operations on the tables stored in a SQL SERVER DB produce a result in the form of a table. SQL SERVER 2000 has introduced features, such as URL Query and XPath queries, for integrating XML with SQL Server.</p>
<p>* FOR XML clause: The FOR XML clause of the SELECT statement can be used to generate an XML document based on the data returned by a query.</p>
<p>* OPENXML function: It provide the OPENXML function that can be used in a T-SQL statement. This function has the ability to represent an XML document as a rowset.</p>
<p>* Direct URL Queries: The SQL Sever XML Support for IIS utility is used to process direct URL queries. This utility parses the query and sends it to SQL server, which generate the result as an XML document.</p>
<p>* XPath Queries: An XPath Query is an XML-based query language that can be used to process the data in XML documents. The XML Support for IIS utility of SQL Server 2000 can be used to process XPath queries and generate the result as an XML Document.</p>
<p><strong>Generating an XML Document</strong></p>
<p>SQL Server 2000 provides the SQL-XML mapping rules for extracting the XML data from DB. You can use FOR XML clause of the select statement to extract data from the DB and display it as an XML Document.</p>
<p>In FOR XML Clause, data are retrieved in 3 different modes such as AUTO, RAW, and EXPLICIT.</p>
<p><strong>Syntax for FOR XML</strong></p>
<p>[source:sql]</p>
<p>SELECT *<br />
FROM table_name<br />
FOR XML mode_type</p>
<p>[/source]</p>
<p><strong>AUTO MODE</strong>: The AUTO mode returns the query result as nested XML element.</p>
<p><strong>RAW MODE</strong>: The RAW mode takes the query result and transforms each row in the result set into an XML element with a generic row identifier as the element tag. This mode maps the columns that have non-null values to an attribute of an XML element.</p>
<p><strong>EXPLICIT</strong>: If you specify the EXPLICIT mode, you must ensure that the generated XML document is well-formed and valid. An EXPLICIT mode query produces a universal table that contains all the information about resultant XML tree.</p>
<p>As I stated earlier, you can use the SELECT statement with the FOR XML clause to generate an XML document from the db. If you want to display the resulting XML document in a browser, you need to compose the URL according to the requirement.</p>
<p><strong>Syntax of a URL query</strong>: http:///?sql=&amp;root=root_element_name</p>
<p>To use this URL in the browser, you need to create the web site on a web server. A web server can be implemented by installing IIS in any windows OS. After that you need to create the virtual directory and that virtual directory map to physical directory.</p>
<p>SQLSERVER also allows you to create the a virtual directory on a web site by using the Configure SQL XML Support in IIS option in the SQL SERVER program group.</p>
<p><strong>Steps to create Virtual Directory:</strong></p>
<p>1. Create a folder called stores (depends on you) under the c:\inetpub\wwroot directory.<br />
2. Select the Configure SQL XML Support in IIS option from Microsoft SQL SERVER program group. This will open the IIS virtual directory management. Right click the default web page site node in the tree displayed. Select New-&gt;Virtual Directory-&gt;this will invoke the new virtual directory properties window.<br />
Click the general tab. Type the name of the new virtual dir as stores. Set the physical path of the virtual directoryÂ  to c:\inetpub\wwroot\stores<br />
3. Then select the security tab, specify the User Name and Password to be used to connect the sql server.<br />
4. Then select the data source tab, and specify the Database name and server name to be associated with the virtual dir.<br />
5. Click the Setting tab. Check the Allow URL Queries and Allow XPath check boxes to enable a user to send URL and Xpath queries from the browser.<br />
6. Click Ok and close the IIS Virtual Directory Management.</p>
<p><strong>Execution Part</strong>: http://localhost/stores?sql=SELECT+*+FROM+sales+FOR+XML+AUTO&amp;root=sales</p>
<p>Copyright @Â  Sathiyasivam 2007</p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/06/20/xml-support-in-microsoft-sql-server.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practical Implementation of MSMQ Using ASP.Net and SQL Server</title>
		<link>http://9tutorials.com/2007/06/07/practical-implementation-of-msmq-using-aspnet-and-sql-server.html</link>
		<comments>http://9tutorials.com/2007/06/07/practical-implementation-of-msmq-using-aspnet-and-sql-server.html#comments</comments>
		<pubDate>Thu, 07 Jun 2007 12:40:56 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[ASP]]></category>
		<category><![CDATA[Database]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/06/07/practical-implementation-of-msmq-using-aspnet-and-sql-server.html</guid>
		<description><![CDATA[Microsoft Message Queuing (MSMQ) technology enables applications running at different times to communicate across different networks and even the systems that may be available. MSMQ component delivery the message without file, efficient routing, security, reliable, and priority-based messaging. Even MSMQ handles both asynchronous and synchronous messaging type queuing.
Due to the high-speed networks, new requirement for [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>Microsoft Message Queuing (MSMQ) technology enables applications running at different times to communicate across different networks and even the systems that may be available.<span id="more-1035"></span> MSMQ component delivery the message without file, efficient routing, security, reliable, and priority-based messaging. Even MSMQ handles both asynchronous and synchronous messaging type queuing.</p>
<p>Due to the high-speed networks, new requirement for technology to cater to a lot of high scale distributed applications. Now a dayâ€™s everything has to be fast and reliable even if the applications are not in the same location. To resolve this Microsoft introduced 3-Tier Arch. Even there is some many draw back in the 3-tier Arch, Consider a situation when so many people trying to register for a course in the university. Due to the over load server canâ€™t able to response to the huge request. Because of that huge request, so many users request are rejected. To avoid this Microsoft Introduced MSMQ. MSMQ allows the application running at different times to communicate across different network and even the systems that may be available. It supports reliable transaction.<br />
It enables the application to communicate across different network; even the server is temporally not available. Client Applications send and stores the messages to queues and read messages from queues.</p>
<p><strong>What is a message? </strong><br />
A message is a collection data or chunk of information that is sent from a process running on client pc to other running process on the same or different computers on different network.</p>
<p><strong>Queue? </strong><br />
Queues are nothing but First-In-First-Out (FIFO) data structures. Queues can be created using the MSMQ management tool through code or manually. Queues can be created and used dynamically. However you need to be aware of issues that might be caused by replication delays in the case of public queues. Private queues does not have such issues And there is no limitation for queues in MSMQ. You can able to create â€˜nâ€™ number of queues.</p>
<p><strong>Types of Queues: </strong></p>
<ul>
<li>Public queues are those that are published in the MQIS and are replicated throughout the Enterprise. They can hence be located by any computer on the Enterprise.</li>
<li>Private queues on the other hand are not published in the MQIS and can only be accessed by systems that have access to the full path name or format name of the queue.</li>
</ul>
<ul>
<li><strong>Store and Forward Communication </strong><br />
It enables applications to send requests to other applications even the destination is unreachable at the same time. It queued (store) the application and process the request once the destination is reachable.</li>
<li><strong>Concurrent Execution</strong><br />
It allows sending request to heterogeneous network without getting acknowledged from the destination. We can also compare it with parallel process.</li>
</ul>
<ul>
<li>Guaranteed Message Delivery</li>
<li>Asynchronous communication</li>
<li>Transaction support</li>
<li>Message routing services</li>
<li>High scalability</li>
<li>Integration with Windows NT Security System</li>
</ul>
<p><strong>Where do we need? </strong></p>
<p><strong>MSMQ features </strong></p>
<p><strong>Sample Demo Application </strong><br />
This sample application to support people to register for application. And this application provide User interface using ASP.NET. Once the User completed entering the message and the message called the MessageQueue, which in turn call the â€œregistration â€œ which is a private queue in my local system. A server Application implementing MSMQ provides the Message Processing by adding this information to a database and sending a response email.</p>
<p><strong>Client Code to store MSMQ Message:</strong></p>
<p>[source:c#]</p>
<p>try<br />
{<br />
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue (path.Text);</p>
<p>mq.DefaultPropertiesToSend.Recoverable=true;<br />
mq.DefaultPropertiesToSend.Label=&#8221;registration&#8221;;<br />
//Test myTest = new  Test(txtuserid.Text,txtpass.Text,txtcpass.Text,txtname.Text,txtcname.Text,txtadd.Text,txtcity.Text,txtcon.Text,txtemail.Text);<br />
Test myTest = new  Test(txtuserid.Text,txtpass.Text,txtcpass.Text,txtname.Text,txtcname.Text,txtadd.Text,txtcity.Text,txtcon.Text,txtemail.Text,phone.Text);<br />
System.Messaging.Message msg=new System.Messaging.Message();<br />
msg.Formatter =new System.Messaging.BinaryMessageFormatter();<br />
msg.Body=myTest;<br />
//txtadd.Text=&#8221;123&#8243;;<br />
mq.Send(msg,&#8221;registration&#8221;,System.Messaging.MessageQueueTransactionType.Single);</p>
<p>//  Code to send email<br />
string strTo = txtemail.Text;<br />
string strFrom = &#8220;Your _mail@rediffmail.com&#8221;;<br />
string strSubject = &#8220;Thank you &#8220;+txtname.Text+&#8221; for testing my demo application&#8221;;<br />
string message=&#8221;Dear &#8220;+txtname.Text+&#8221;,&#8221;+&#8221;\n\n&#8221;+&#8221;Thank you for testing my demo application. I hope you understand the scope of MSMQ.&#8221;+&#8221;\n\n&#8221;+&#8221;Thanks\n&#8221;+&#8221;S.SathiyaSivam&#8221;;<br />
SmtpMail.SmtpServer = &#8220;localhost&#8221;;<br />
System.Web.Mail.SmtpMail.SmtpServer = &#8220;localhost&#8221;;<br />
SmtpMail.Send(strFrom, strTo, strSubject,<br />
message);</p>
<p>}</p>
<p>[/source]</p>
<p><strong>Code to retrieve from message from queue:</strong></p>
<p>[source:c#]</p>
<p>MessageQueue qÂ  = new MessageQueue(@&#8221;.\Private$\registration&#8221;);<br />
Test tst=null;<br />
if(MessageId!=null)<br />
{<br />
// need to strip off the curly brackets to use ReceiveById!<br />
string msgid=((string)MessageId).Replace(&#8220;}&#8221;,&#8221;");<br />
msgid=msgid.Replace(&#8220;{&#8220;,&#8221;");<br />
msg= q.ReceiveById(msgid);<br />
Type[] expectedTypes= new Type[]{typeof(string)};<br />
msg.Formatter =new System.Messaging.BinaryMessageFormatter();<br />
System.Diagnostics.Debug.WriteLine(msg.Id.ToString());<br />
tst=(Test)msg.Body;<br />
}</p>
<p>MessageBox.Show(&#8220;Your Request Is Processed From The Queue&#8221;);<br />
string sSql=&#8221;Insert into registration (UserId,Pass,Cpass,Name,Cname,Padd,city,Country,Email,Phone)Values(&#8216;&#8221;+tst.Uid+&#8221;&#8216;,&#8217;&#8221;+tst.Pass+&#8221;&#8216;,&#8217;&#8221;+tst.Cpass +&#8221;&#8216;,&#8217;&#8221;+tst.Uname+&#8221;&#8216;,&#8217;&#8221;+tst.Cname+&#8221;&#8216;,&#8217;&#8221;+tst.Add+&#8221;&#8216;,&#8217;&#8221;+tst.City+&#8221;&#8216;,&#8217;&#8221;+tst.Con+&#8221;&#8216;,&#8217;&#8221;+tst.Email+&#8221;&#8216;,&#8217;&#8221;+tst.Phone+&#8221;&#8216;)&#8221;;<br />
System.Diagnostics.Debug.WriteLine(sSql);<br />
SqlConnection cn =new SqlConnection(&#8220;Server=.;Database=TEST;Trusted_Connection=True;&#8221;);<br />
cn.Open();<br />
SqlCommand cmd=new SqlCommand(sSql,cn);<br />
cmd.CommandType=CommandType.Text;<br />
cmd.ExecuteNonQuery();<br />
cmd.Dispose();<br />
cn.Close();</p>
<p>[/source]<br />
<strong>Steps to create private queue :</strong></p>
<ol>
<li>Create a new Private Queue called &#8220;registrtion&#8221;</li>
<li>Check the Transactional Queue check box in the Private Queue window.</li>
<li>Create a new Trigger for Peeking, enabled, serialized, called &#8220;Registration&#8221;</li>
<li>Create a new Rule called &#8220;reg_rule&#8221; with a Rule Condition of &#8220;Message Label contains registration&#8221;</li>
<li>For the Rule Action, specify the path to the backend project executable (on my machine, this is &#8220;C:\CSHARPBIN\registration\backend\bin\Debug\backend.exe&#8221;</li>
<li>In Parameters, pass the MessageId as Variant, and the MessageBody as String.</li>
<li>In your Trigger&#8217;s attached rules, ensure that the Rule Notify is attached.</li>
<li>Ensure that both the MessageQueue and the MessageQueue Trigger services are running.</li>
</ol>
<p><em>Copyright @ SathiyasivamÂ  2007</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/06/07/practical-implementation-of-msmq-using-aspnet-and-sql-server.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating an object oriented MySQL abstraction class</title>
		<link>http://9tutorials.com/2007/05/26/creating-an-object-oriented-mysql-abstraction-class.html</link>
		<comments>http://9tutorials.com/2007/05/26/creating-an-object-oriented-mysql-abstraction-class.html#comments</comments>
		<pubDate>Sun, 27 May 2007 02:47:35 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/26/creating-an-object-oriented-mysql-abstraction-class.html</guid>
		<description><![CDATA[In this tutorial I will show you how you can create an object oriented database abstraction class for MySQL database. Besides the basic functions this manager class can summarize total time spent with db connection and query execution and counts how many query were executed.
Step 1.
The first question is why we need at all such [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>In this tutorial I will show you how you can create an object oriented database abstraction class for MySQL database. Besides the basic functions this manager class can summarize total time spent with db connection and query execution and counts how many query were executed.<span id="more-576"></span><br />
<strong>Step 1.</strong><br />
The first question is why we need at all such script if we have all function in PHP as default. There are more arguments:</p>
<ul>
<li class="MsoNormal">     You can easy reuse the script in all of your development.</li>
<li class="MsoNormal">     You can change the database without changing all of your script. You need to edit only the abstraction class.</li>
<li class="MsoNormal">     You can make optimalisation and simplification more easy.</li>
<li class="MsoNormal">     You can centralize error handling and db access.</li>
<li class="MsoNormal">     You can make the debugging easier.</li>
<li class="MsoNormal">     You can easy implement query time statistics and query counters.</li>
</ul>
<p>Now let&#8217;s summarize what kind of function do we need:</p>
<ul>
<li class="MsoNormal">     Creating a connection to the database</li>
<li class="MsoNormal">     Executing sql queries</li>
<li class="MsoNormal">     Get the number of selected rows if possible</li>
<li class="MsoNormal">     Get the number of modified rows if possible</li>
<li class="MsoNormal">     Get the auto increment id of the latest insert statement if possible</li>
<li class="MsoNormal">     Load a query result into a normal array</li>
<li class="MsoNormal">     Get the error message and/or id if necessary</li>
<li class="MsoNormal">     Get the total time spent in the database</li>
<li class="MsoNormal">     Get the number of the executed queries</li>
</ul>
<p><strong>Step 2.<br />
</strong>Before we can start the implementation of the functions we need to collect all common used class variables which stores important information.<br />
These are:</p>
<ul>
<li class="MsoNormal">     A connection identifier to store the actual connection link.</li>
<li class="MsoNormal">     A result Set to store query results.</li>
<li class="MsoNormal">     An error message and error id to store error information.</li>
<li class="MsoNormal">     A counter to count executed queries.</li>
<li class="MsoNormal">     A time counter to store total query times.</li>
</ul>
<p>So the basic DB manager class looks like this:</p>
<p>[source:php]</p>
<p><span style="color: #000000"> <span style="color: #0000bb">&lt;?php</span></span></p>
<p><span style="color: #007700">class </span><span style="color: #0000bb">DBManager</span><span style="color: #007700">{</span></p>
<p>var <span style="color: #0000bb">$connection   </span><span style="color: #007700">= </span><span style="color: #dd0000">&#8221;</span><span style="color: #007700">;<br />
var </span><span style="color: #0000bb">$queryCounter </span><span style="color: #007700">= </span><span style="color: #0000bb">0</span><span style="color: #007700">;<br />
var </span><span style="color: #0000bb">$totalTime    </span><span style="color: #007700">= </span><span style="color: #0000bb">0</span><span style="color: #007700">;<br />
var </span><span style="color: #0000bb">$errorCode    </span><span style="color: #007700">= </span><span style="color: #0000bb">0</span><span style="color: #007700">;<br />
var </span><span style="color: #0000bb">$errorMsg     </span><span style="color: #007700">= </span><span style="color: #dd0000">&#8221;</span><span style="color: #007700">;<br />
var </span><span style="color: #0000bb">$resultSet    </span><span style="color: #007700">= </span><span style="color: #dd0000">&#8221;</span><span style="color: #007700">;</span></p>
<p>}<br />
<span style="color: #0000bb">?&gt;</span></p>
<p>[/source]</p>
<p><strong>Step 3.<br />
</strong>Now we can implement the functions. Let&#8217;s start with the most important one. Connecting to the database. We will create no stand alone connect script but the constructor of the class will be responsible to build up the database connection. So if you create a database object from this class you immediately have a valid connection as well.</p>
<p>So the constructor needs the following parameters:</p>
<ul>
<li class="MsoNormal">     hostname</li>
<li class="MsoNormal">     username</li>
<li class="MsoNormal">     password</li>
<li class="MsoNormal">     database name</li>
</ul>
<p>In the function first we try to connect to the database server and if it was success than we try to select the given database. In case of any error we set the error message and error id variables and return with false.</p>
<p>The constructor of the class is the following:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">DBManager</span><span style="color: #007700">(</span><span style="color: #0000bb">$host</span><span style="color: #007700">, </span><span style="color: #0000bb">$user</span><span style="color: #007700">, </span><span style="color: #0000bb">$pass</span><span style="color: #007700">, </span><span style="color: #0000bb">$db</span><span style="color: #007700">){<br />
</span><span style="color: #0000bb">$startTime </span><span style="color: #007700">= </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">getMicroTime</span><span style="color: #007700">();</span></span></p>
<p><span style="color: #ff8000">// Try to make a connection to the server<br />
</span><span style="color: #007700">if (!</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection </span><span style="color: #007700">= @</span><span style="color: #0000bb">mysql_connect</span><span style="color: #007700">(</span><span style="color: #0000bb">$host</span><span style="color: #007700">,</span><span style="color: #0000bb">$user</span><span style="color: #007700">,</span><span style="color: #0000bb">$pass</span><span style="color: #007700">,</span><span style="color: #0000bb">true</span><span style="color: #007700">)){<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorCode </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_errno</span><span style="color: #007700">();<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorMsg  </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_error</span><span style="color: #007700">();<br />
return </span><span style="color: #0000bb">false</span><span style="color: #007700">;<br />
}</span></p>
<p><span style="color: #ff8000">// Now select the database<br />
</span><span style="color: #007700">if (!@</span><span style="color: #0000bb">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000bb">$db</span><span style="color: #007700">,</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection</span><span style="color: #007700">)){<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorCode </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_errno</span><span style="color: #007700">();<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorMsg  </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_error</span><span style="color: #007700">();<br />
@</span><span style="color: #0000bb">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection</span><span style="color: #007700">);<br />
return </span><span style="color: #0000bb">false</span><span style="color: #007700">;<br />
}</span></p>
<p><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">totalTime </span><span style="color: #007700">+= </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">getMicroTime</span><span style="color: #007700">() &#8211; </span><span style="color: #0000bb">$startTime</span><span style="color: #007700">;</span></p>
<p>return <span style="color: #0000bb">true</span><span style="color: #007700">;<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span><br />
[/source]<br />
<strong>Step 4.<br />
</strong>As next step we will implement the query execution function. In this function we measure the execution time (see details later), increasing the query counter and of course executing the query itself. If an error occurs than we set the error message and error id variables and returns with false. If everything was working fine we return with the result set.</p>
<p>The code is the following:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000bb">$sql</span><span style="color: #007700">){<br />
</span><span style="color: #0000bb">$startTime </span><span style="color: #007700">= </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">getMicroTime</span><span style="color: #007700">();</span></span></p>
<p>++<span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">queryCounter</span><span style="color: #007700">;</span></p>
<p>if(!<span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">resultSet </span><span style="color: #007700">= @</span><span style="color: #0000bb">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000bb">$sql</span><span style="color: #007700">,</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection</span><span style="color: #007700">)){<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorCode </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_errno</span><span style="color: #007700">();<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorMsg  </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_error</span><span style="color: #007700">();<br />
</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">totalTime </span><span style="color: #007700">= </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">getMicroTime</span><span style="color: #007700">() &#8211; </span><span style="color: #0000bb">$startTime</span><span style="color: #007700">;<br />
return </span><span style="color: #0000bb">false</span><span style="color: #007700">;<br />
}</span></p>
<p><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">totalTime </span><span style="color: #007700">+= </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">getMicroTime</span><span style="color: #007700">() &#8211; </span><span style="color: #0000bb">$startTime</span><span style="color: #007700">;</span></p>
<p>return <span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">resultSet</span><span style="color: #007700">;<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span><br />
[/source]<br />
If the query contains an Insert, Update or Delete command than it can be important to know how many records were affected by this query. To get it we implement a function for that as follows:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">getAffectedRows</span><span style="color: #007700">()<br />
{<br />
return @</span><span style="color: #0000bb">mysql_affected_rows</span><span style="color: #007700">(</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection</span><span style="color: #007700">);<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span> </span><br />
[/source]<br />
In case of a Select statement the number of selected records could be interesting. To get it we have a similar function:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">getSelectedRows</span><span style="color: #007700">()<br />
{<br />
return @</span><span style="color: #0000bb">mysql_num_rows</span><span style="color: #007700">(</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">resultSet</span><span style="color: #007700">);<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span> </span><br />
[/source]<br />
In this topic we have a last important issue. In case of Insert maybe we want to know an auto increment filed value. To get this information after the record was inserted we can use the following code:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">getInsertId</span><span style="color: #007700">(){<br />
return @</span><span style="color: #0000bb">mysql_insert_id</span><span style="color: #007700">(</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">connection</span><span style="color: #007700">);<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span> </span><br />
[/source]<br />
<strong>Step 5.<br />
</strong>Now let&#8217;s create some other useful function to make our class more usable.<br />
In case of a select it sometimes it would be nice to get an associative array instead of the MySQL result set. So we implement a function which returns with such an array generated from the actual result set.<br />
The code looks like this:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">loadResult</span><span style="color: #007700">() {<br />
</span><span style="color: #0000bb">$array </span><span style="color: #007700">= array();<br />
while (</span><span style="color: #0000bb">$row </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_fetch_object</span><span style="color: #007700">( </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">resultSet </span><span style="color: #007700">)) {<br />
</span><span style="color: #0000bb">$array</span><span style="color: #007700">[] = </span><span style="color: #0000bb">$row</span><span style="color: #007700">;<br />
}<br />
</span><span style="color: #0000bb">mysql_free_result</span><span style="color: #007700">( </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">resultSet </span><span style="color: #007700">);</span></span></p>
<p>return <span style="color: #0000bb">$array</span><span style="color: #007700">;<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span><br />
[/source]<br />
As our function don&#8217;t send error messages back to the caller just sets the error variables inside the class so we need function to get this information. These are simple getter functions as follows:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">getErrrorCode</span><span style="color: #007700">(){<br />
return </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorCode</span><span style="color: #007700">;<br />
}</span></span></p>
<p>function <span style="color: #0000bb">getErrorMessage</span><span style="color: #007700">(){<br />
return </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">errorMsg</span><span style="color: #007700">;<br />
}<br />
</span><span style="color: #0000bb">?&gt;<br />
</span> [/source]</p>
<p><strong>Step 6.</strong><br />
As final step we need to implement a function to get query times. To do it we need a function which returns the actual time in milliseconds. With calling this function at the beginning and at the end of the affected db functions we can calculate the total db time. Besides this we implement 2 other getter functions to get the number of executed queries and the total query time in a usable format.</p>
<p>So our helper functions looks like this:<br />
[source:php]<br />
<span style="color: #000000"> <span style="color: #0000bb">&lt;?php<br />
</span><span style="color: #007700">function </span><span style="color: #0000bb">getDBTime</span><span style="color: #007700">(){<br />
return </span><span style="color: #0000bb">round</span><span style="color: #007700">(</span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">totalTime</span><span style="color: #007700">,</span><span style="color: #0000bb">6</span><span style="color: #007700">);<br />
}</span></span></p>
<p>function <span style="color: #0000bb">getSqlCount</span><span style="color: #007700">(){<br />
return </span><span style="color: #0000bb">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000bb">queryCounter</span><span style="color: #007700">;<br />
}</span></p>
<p>function <span style="color: #0000bb">getMicroTime</span><span style="color: #007700">() {<br />
list(</span><span style="color: #0000bb">$usec</span><span style="color: #007700">, </span><span style="color: #0000bb">$sec</span><span style="color: #007700">) = </span><span style="color: #0000bb">explode</span><span style="color: #007700">(</span><span style="color: #dd0000">&#8221; &#8220;</span><span style="color: #007700">,</span><span style="color: #0000bb">microtime</span><span style="color: #007700">());<br />
return ((float)</span><span style="color: #0000bb">$usec </span><span style="color: #007700">+ (float)</span><span style="color: #0000bb">$sec</span><span style="color: #007700">);<br />
}<br />
</span><span style="color: #0000bb">?&gt;</span></p>
<p>[/source]</p>
<p><em>Copyright @ PHP Toys 2007</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/26/creating-an-object-oriented-mysql-abstraction-class.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>8 Essential MySQL Queries</title>
		<link>http://9tutorials.com/2007/05/13/8-essential-mysql-queries.html</link>
		<comments>http://9tutorials.com/2007/05/13/8-essential-mysql-queries.html#comments</comments>
		<pubDate>Mon, 14 May 2007 00:24:47 +0000</pubDate>
		<dc:creator>dangtruong</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[essential]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://9tutorials.com/2007/05/13/8-essential-mysql-queries.html</guid>
		<description><![CDATA[Here&#8221;s a list of queries that I found myself using very often and that save me a lot of development time. I hope you can benefit from them as well.
1. Create a quick backup
Before testing a new piece of code you suspect might mess up data in one or more tables it&#8221;s always a good [...]]]></description>
			<content:encoded><![CDATA[<div class="spost"><p>Here&#8221;s a list of queries that I found myself using very often and that save me a lot of development time. I hope you can benefit from them as well.<span id="more-65"></span></p>
<p><em>1. Create a quick backup</em></p>
<p>Before testing a new piece of code you suspect might mess up data in one or more tables it&#8221;s always a good practice to create a backup. To quickly create a backup copy of a table, use this query:</p>
<p>[source:sql]</p>
<p>CREATE TABLE backup SELECT * FROM TABLE original;</p>
<p>[/source]</p>
<p>The query creates a table backup which is a copy of the original table and includes both structure and content.</p>
<p><em>2. Create/change/restore a password</em></p>
<p>Many applications store MD5-crypted passwords in the database. If you want to quickly create a new MD5-ed password, or you have forgotten your password, use the following query to get a new one:</p>
<p>[source:sql]</p>
<p>SELECT MD5(&#8220;somepasshere&#8221;);</p>
<p>[/source]</p>
<p>This statement will give you &#8220;b5bab206cc8002bf7c10d47b24a2d0e6&#8243; which is the encrypted version of the string &#8220;somepasshere&#8221;. There are other function that crypt stings in MySQL using different algorithms, most notably PASSWORD() which is using MySQL&#8221;s own crypting algorithm.</p>
<p><em>3. Working with Unix timestamps</em></p>
<p>To convert from human-readable MySQL date/time format into Unix timestamp, use: SELECT UNIX_TIMESTAMP(); Without parameters, this will give you the timestamp of the current date and time. With parameters, you can get timestamp for any date. For example: SELECT UNIX_TIMESTAMP(&#8220;2006-12-31&#8243;); This statement gives you the timestamp 1167541200. To get a date back from a timestamp, use: SELECT FROM_UNIXTIME(1167541200); This results in &#8220;2006-12-31 00:00:00&#8243;</p>
<p><em>4. Quick increment</em></p>
<p>To increment an integer stored in a table (useful for stats for example), use: UPDATE sometable SET counter=counter+1 WHERE Ã¢â‚¬Â¦; Here &#8220;counter&#8221; is the name of the field that stores the integer value.<br />
<!--adsense#inside--><br />
<em>5. Toggle a value</em></p>
<p>If you have a field that stores a Boolean type of value, like 0/1 or yes/no, you can easily toggle the value with one if-statement:</p>
<p>[source:sql]</p>
<p>UPDATE sometable SET flag=(IF(flag=&#8221;no&#8221;,&#8221;yes&#8221;,&#8221;no&#8221;));</p>
<p>[/source]</p>
<p><em>6. Find/replace</em></p>
<p>Say you want to update a piece of text if all records in a table field. REPLACE() comes to the rescue:</p>
<p>[source:sql]</p>
<p>UPDATE sometable SET field = REPLACE(field, &#8220;black&#8221;,&#8221;white&#8221;);</p>
<p>[/source]</p>
<p>This statement will replace all occurrences of the string &#8220;black&#8221; with the string &#8220;white&#8221; in all records of the &#8220;field&#8221; column. Apart from the string &#8220;white&#8221; the rest of the text contained in the field will be left as is.</p>
<p><em>7. Get a random record</em></p>
<p>If you want to select a random row in your table, you can use the statement:</p>
<p>[source:sql]</p>
<p>SELECT * FROM table ORDER BY RAND();</p>
<p>[/source]</p>
<p><em>8. Upper/lower case</em></p>
<p>If you want to modify a value and make it upper or lowercase, use the UPPER or LOWER functions, like this:</p>
<p>[source:sql]</p>
<p>SELECT LOWER(&#8220;Value&#8221;); // gives you &#8220;value&#8221;</p>
<p>SELECT UPPER(&#8220;Value&#8221;); // gives you &#8220;VALUE&#8221;</p>
<p>[/source]</p>
<p>I hope you learned something new today which will make your everyday life as a developer just a bit easier. Thank you for reading!</p>
<p><em>Author: Stoyan Stefanov is a web developer from Montreal, Canada, Zend Certified Engineer</em></p>
]]></content:encoded>
			<wfw:commentRss>http://9tutorials.com/2007/05/13/8-essential-mysql-queries.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
