<?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>JaffnaCampus.com &#187; PHP Database</title>
	<atom:link href="http://jaffnacampus.com/category/php/php-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://jaffnacampus.com</link>
	<description>Gateway to your IT potential</description>
	<lastBuildDate>Tue, 27 Sep 2011 13:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Database ODBC</title>
		<link>http://jaffnacampus.com/php/php-database/php-database-odbc/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-database-odbc/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:36:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP Database ODBC]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=353</guid>
		<description><![CDATA[ODBC is an Application Programming Interface   (API) that allows you to connect to a data source (e.g. an [...]]]></description>
			<content:encoded><![CDATA[<p>ODBC is an Application Programming Interface   (API) that allows you to connect to a data source (e.g. an MS   Access database).</p>
<hr />
<h2>Create an ODBC Connection</h2>
<p>With an ODBC connection, you can connect to any database, on any   computer in   your network, as long as an ODBC connection is available.</p>
<p>Here is how to create an ODBC connection to a MS Access Database: </p>
<ol>
<li>Open the <strong>Administrative Tools</strong> icon in your Control Panel.</li>
<li>Double-click on the <strong>Data Sources (ODBC)</strong> icon inside. </li>
<li>Choose the <strong>System DSN</strong> tab. </li>
<li>Click on <strong>Add </strong>in the System DSN tab. </li>
<li>Select the <strong>Microsoft Access Driver</strong>. Click <strong>Finish.</strong></li>
<li>In the next screen, click <strong>Select</strong> to locate the database. </li>
<li>Give the database a <strong>Data Source Name (DSN)</strong>. </li>
<li>Click <strong>OK</strong>.</li>
</ol>
<p>Note that this configuration has to be done on the computer where   your web   site is located. If you are running Internet   Information Server (IIS) on your own computer, the instructions above   will work,   but if your web site is located on a remote server, you have to have   physical   access to that server, or ask your web host to to set up a DSN for   you to use.</p>
<hr />
<h2>Connecting to an ODBC</h2>
<p>The odbc_connect() function is used to connect to an ODBC data   source. The function takes   four parameters: the data source name, username,   password, and an optional cursor type.</p>
<p>The odbc_exec() function is used to execute an SQL statement.</p>
<h3>Example</h3>
<p>The following example creates a connection to a DSN   called northwind, with no username and no password. It then creates an   SQL and   executes it:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> $conn=odbc_connect(&#8216;northwind&#8217;,&#8221;,&#8221;);<br />
        $sql=&quot;SELECT * FROM customers&quot;;<br />
        $rs=odbc_exec($conn,$sql); </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Retrieving Records</h2>
<p>The odbc_fetch_row() function is used to return records from the   result-set. This function returns true if it is able to return rows,   otherwise   false.</p>
<p>The function takes two parameters: the ODBC result identifier and an   optional   row number:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> odbc_fetch_row($rs) </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Retrieving Fields from a Record</h2>
<p>The odbc_result() function is used to read fields from a record. This   function   takes two parameters: the ODBC   result identifier and a field number or name.</p>
<p>The code line below returns the value of the first field from the   record: </p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> $compname=odbc_result($rs,1); </td>
</tr>
</tbody>
</table>
<p>The code line below returns the value of a field called   &quot;CompanyName&quot;: </p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> $compname=odbc_result($rs,&quot;CompanyName&quot;); </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Closing an ODBC Connection</h2>
<p>The odbc_close() function is used to close an ODBC connection.</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> odbc_close($conn); </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>An ODBC Example</h2>
<p>The following example shows how to first create a database   connection, then a   result-set, and then display the data in an HTML table.</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;?php<br />
        $conn=odbc_connect(&#8216;northwind&#8217;,&#8221;,&#8221;);<br />
        if (!$conn)<br />
        {exit(&quot;Connection Failed: &quot; . $conn);}<br />
        $sql=&quot;SELECT * FROM customers&quot;;<br />
        $rs=odbc_exec($conn,$sql);<br />
        if (!$rs)<br />
        {exit(&quot;Error in SQL&quot;);}<br />
        echo &quot;&lt;table&gt;&lt;tr&gt;&quot;;<br />
        echo &quot;&lt;th&gt;Companyname&lt;/th&gt;&quot;;<br />
        echo &quot;&lt;th&gt;Contactname&lt;/th&gt;&lt;/tr&gt;&quot;;<br />
        while (odbc_fetch_row($rs))<br />
        {<br />
        $compname=odbc_result($rs,&quot;CompanyName&quot;);<br />
        $conname=odbc_result($rs,&quot;ContactName&quot;);<br />
        echo &quot;&lt;tr&gt;&lt;td&gt;$compname&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;$conname&lt;/td&gt;&lt;/tr&gt;&quot;;<br />
        }<br />
        odbc_close($conn);<br />
        echo &quot;&lt;/table&gt;&quot;;<br />
        ?&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-database-odbc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Delete</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-delete/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-delete/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:34:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Delete]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=351</guid>
		<description><![CDATA[The DELETE statement is used to delete records in a   table.

Delete Data In a Database
The DELETE FROM statement [...]]]></description>
			<content:encoded><![CDATA[<p>The DELETE statement is used to delete records in a   table.</p>
<hr />
<h2>Delete Data In a Database</h2>
<p>The DELETE FROM statement is used to delete records from a database   table.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> DELETE FROM table_name<br />
        WHERE some_column = some_value </td>
</tr>
</tbody>
</table>
<p>
<strong>Note:</strong> Notice the WHERE clause in the DELETE syntax. The WHERE   clause   specifies which record or records that should be deleted. If you omit   the WHERE   clause, all records will be deleted!</p>
<p>To get PHP to execute the statement above we must use the   mysql_query() function. This function is used to   send a query or command to a MySQL connection.</p>
<h3>Example</h3>
<p>Look at the following &quot;Persons&quot; table:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>35</td>
</tr>
<tr>
<td>Glenn</td>
<td>Quagmire</td>
<td>33</td>
</tr>
</tbody>
</table>
<p>The following example deletes all the records in the &quot;Persons&quot; table   where LastName=&#8217;Griffin&#8217;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        mysql_query(&quot;DELETE FROM Persons WHERE LastName=&#8217;Griffin&#8217;&quot;);</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>After the deletion, the table will look like this:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
</tr>
<tr>
<td>Glenn</td>
<td>Quagmire</td>
<td>33</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-delete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Update</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-update/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-update/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:33:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Update]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=349</guid>
		<description><![CDATA[The UPDATE statement is used to modify data in a table.

Update Data In a Database
The UPDATE statement is used to [...]]]></description>
			<content:encoded><![CDATA[<p>The UPDATE statement is used to modify data in a table.</p>
<hr />
<h2>Update Data In a Database</h2>
<p>The UPDATE statement is used to update existing records in a table.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> UPDATE table_name<br />
        SET column1=value, column2=value2,&#8230;<br />
        WHERE some_column=some_value </td>
</tr>
</tbody>
</table>
<p>
<strong>Note:</strong> Notice the WHERE clause in the UPDATE syntax. The WHERE   clause   specifies which record or records that should be updated. If you omit   the WHERE   clause, all records will be updated!</p>
<p>To get PHP to execute the statement above we must use the   mysql_query() function. This function is used to   send a query or command to a MySQL connection.</p>
<h3>Example</h3>
<p>Earlier in the tutorial we created a table named &quot;Persons&quot;. Here is   how it   looks:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>35</td>
</tr>
<tr>
<td>Glenn</td>
<td>Quagmire</td>
<td>33</td>
</tr>
</tbody>
</table>
<p>The following example updates some data in the &quot;Persons&quot; table:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        mysql_query(&quot;UPDATE Persons SET Age = &#8216;36&#8242;<br />
        WHERE FirstName = &#8216;Peter&#8217; AND LastName = &#8216;Griffin&#8217;&quot;);</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>After the update, the &quot;Persons&quot; table will look like this:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>36</td>
</tr>
<tr>
<td>Glenn</td>
<td>Quagmire</td>
<td>33</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Order By Keyword</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-order-by-keyword/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-order-by-keyword/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:32:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Order By Keyword]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-database/php-mysql-order-by-keyword/</guid>
		<description><![CDATA[The ORDER BY keyword is used to sort the data in a   recordset.

The ORDER BY Keyword
The ORDER BY [...]]]></description>
			<content:encoded><![CDATA[<p>The ORDER BY keyword is used to sort the data in a   recordset.</p>
<hr />
<h2>The ORDER BY Keyword</h2>
<p>The ORDER BY keyword is used to sort the data in a recordset.</p>
<p>The ORDER BY keyword sort the records in ascending order by default.</p>
<p>If you want to sort the records in a descending order, you can use   the DESC   keyword.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> SELECT column_name(s)<br />
        FROM table_name<br />
        ORDER BY column_name(s) ASC|DESC </td>
</tr>
</tbody>
</table>
<h3>Example</h3>
<p>The following example selects all the data stored in the &quot;Persons&quot;   table,   and sorts the result by the &quot;Age&quot; column:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        $result = mysql_query(&quot;SELECT * FROM Persons ORDER BY age&quot;);</p>
<p>        while($row = mysql_fetch_array($result))<br />
        {<br />
        echo $row['FirstName'];<br />
        echo &quot; &quot; . $row['LastName'];<br />
        echo &quot; &quot; . $row['Age'];<br />
        echo &quot;&lt;br /&gt;&quot;;<br />
        }</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> Glenn Quagmire 33<br />
        Peter Griffin 35 </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Order by Two Columns</h2>
<p>It is also possible to order by more than one column. When ordering   by more than one column, the second column is only used if   the values in the first column are equal:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> SELECT column_name(s)<br />
        FROM table_name<br />
        ORDER BY column1, column2 </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-order-by-keyword/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Select</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-select/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-select/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:29:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Select]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-advanced/php-mysql-select/</guid>
		<description><![CDATA[The SELECT statement is used to select data from a   database.

Select Data From a Database Table
The SELECT statement [...]]]></description>
			<content:encoded><![CDATA[<p>The SELECT statement is used to select data from a   database.</p>
<hr />
<h2>Select Data From a Database Table</h2>
<p>The SELECT statement is used to select data from a database.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> SELECT column_name(s)<br />
        FROM table_name </td>
</tr>
</tbody>
</table>
<p>To get PHP to execute the statement above we must use the   mysql_query() function. This function is used to   send a query or command to a MySQL connection.</p>
<h3>Example</h3>
<p>The following example selects all the data stored in the &quot;Persons&quot;   table   (The * character selects all the data in the table):</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        $result = mysql_query(&quot;SELECT * FROM Persons&quot;);</p>
<p>        while($row = mysql_fetch_array($result))<br />
        {<br />
        echo $row['FirstName'] . &quot; &quot; . $row['LastName'];<br />
        echo &quot;&lt;br /&gt;&quot;;<br />
        }</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The example above stores the data returned by the mysql_query()   function in the $result variable.</p>
<p>Next, we use the mysql_fetch_array() function to return the first row   from the recordset as an   array. Each call to mysql_fetch_array() returns the next row in   the recordset. The while loop loops through all the records in the   recordset. To print the value of each row, we use the PHP $row variable   ($row['FirstName'] and $row['LastName']).</p>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> Peter Griffin<br />
        Glenn Quagmire </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Display the Result in an HTML Table</h2>
<p>The following example selects the same data as the example above, but   will   display the data in an HTML table:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        $result = mysql_query(&quot;SELECT * FROM Persons&quot;);</p>
<p>        echo <strong>&quot;</strong>&lt;table border=&#8217;1&#8242;&gt;<strong><br />
          &lt;</strong>tr&gt;<br />
        &lt;th&gt;Firstname&lt;/th&gt;<br />
        &lt;th&gt;Lastname&lt;/th&gt;<br />
        &lt;/tr&gt;&quot;;</p>
<p>        while($row = mysql_fetch_array($result))<br />
        {<br />
        echo &quot;&lt;tr&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['FirstName'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['LastName'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;/tr&gt;&quot;;<br />
        }<br />
        echo &quot;&lt;/table&gt;&quot;;</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="1">
<tbody>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Glenn</td>
<td>Quagmire</td>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-select/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Insert Into</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-insert-into/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-insert-into/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:28:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Insert Into]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=344</guid>
		<description><![CDATA[The INSERT INTO statement is used to insert new records   in a table.

Insert Data Into a Database Table
The [...]]]></description>
			<content:encoded><![CDATA[<p>The INSERT INTO statement is used to insert new records   in a table.</p>
<hr />
<h2>Insert Data Into a Database Table</h2>
<p>The INSERT INTO statement is used to add new records to a database   table.</p>
<h3>Syntax</h3>
<p>It is possible to write the INSERT INTO statement in two forms. </p>
<p>The first form doesn&#8217;t specify the column names where the data will   be   inserted, only their values:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> INSERT INTO table_name<em><br />
      </em>VALUES (value1, value2, value3,&#8230;) </td>
</tr>
</tbody>
</table>
<p>The second form specifies both the column names and the values to be   inserted:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> INSERT INTO table_name (column1, column2, column3,&#8230;)<br />
        VALUES (value1, value2, value3,&#8230;) </td>
</tr>
</tbody>
</table>
<p></p>
<p>To get PHP to execute the statements above we must use   the mysql_query() function. This function is used to   send a query or command to a MySQL connection.</p>
<h3>Example</h3>
<p>In the previous chapter we created a table named &quot;Persons&quot;, with   three columns; &quot;Firstname&quot;, &quot;Lastname&quot; and   &quot;Age&quot;. We will use the same table in this example. The following   example adds two new records to the &quot;Persons&quot; table:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        mysql_query(&quot;INSERT INTO Persons (FirstName, LastName, Age)<br />
        VALUES (&#8216;Peter&#8217;, &#8216;Griffin&#8217;, &#8216;35&#8242;)&quot;);</p>
<p>        mysql_query(&quot;INSERT INTO Persons (FirstName, LastName, Age) <br />
        VALUES (&#8216;Glenn&#8217;, &#8216;Quagmire&#8217;, &#8216;33&#8242;)&quot;);</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Insert Data From a Form Into a Database</h2>
<p>Now we will create an HTML form that can be used to add new records   to the   &quot;Persons&quot; table.</p>
<p>Here is the HTML form:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form action=&quot;insert.php&quot; method=&quot;post&quot;&gt;<br />
        Firstname: &lt;input type=&quot;text&quot; name=&quot;firstname&quot; /&gt;<br />
        Lastname: &lt;input type=&quot;text&quot; name=&quot;lastname&quot; /&gt;<br />
        Age: &lt;input type=&quot;text&quot; name=&quot;age&quot; /&gt;<br />
        &lt;input type=&quot;submit&quot; /&gt;<br />
        &lt;/form&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<p>When a user clicks the submit button in the HTML form in the example   above, the form data is sent to &quot;insert.php&quot;.</p>
<p>The &quot;insert.php&quot; file connects to a database, and retrieves the   values   from the form with the PHP $_POST variables.</p>
<p>Then, the mysql_query() function   executes the INSERT INTO statement, and a new record will be added to   the   &quot;Persons&quot; table.</p>
<p>Here is the &quot;insert.php&quot; page:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;my_db&quot;, $con);</p>
<p>        $sql=&quot;INSERT INTO Persons (FirstName, LastName, Age)<br />
        VALUES<br />
        (&#8216;$_POST[firstname]&#8216;,&#8217;$_POST[lastname]&#8216;,&#8217;$_POST[age]&#8216;)&quot;;</p>
<p>        if (!mysql_query($sql,$con))<br />
        {<br />
        die(&#8216;Error: &#8216; . mysql_error());<br />
        }<br />
        echo &quot;1 record added&quot;;</p>
<p>        mysql_close($con)<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-insert-into/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Create Database and Tables</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-create-database-and-tables/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-create-database-and-tables/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:26:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Create Database and Tables]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=342</guid>
		<description><![CDATA[A database holds one or multiple tables.

Create a Database
The CREATE DATABASE statement is used to create a database in MySQL.
Syntax



 [...]]]></description>
			<content:encoded><![CDATA[<p>A database holds one or multiple tables.</p>
<hr />
<h2>Create a Database</h2>
<p>The CREATE DATABASE statement is used to create a database in MySQL.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> CREATE DATABASE database_name </td>
</tr>
</tbody>
</table>
<p></p>
<p>To get PHP to execute the statement above we must use   the mysql_query() function. This function is used to   send a query or command to a MySQL connection.</p>
<h3>Example</h3>
<p>The following example creates a database called &quot;my_db&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        if (mysql_query(&quot;CREATE DATABASE my_db&quot;,$con))<br />
        {<br />
        echo &quot;Database created&quot;;<br />
        }<br />
        else<br />
        {<br />
        echo &quot;Error creating database: &quot; . mysql_error();<br />
        }</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Create a Table</h2>
<p>The CREATE TABLE statement is used to create a table in MySQL.</p>
<h3>Syntax</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> CREATE TABLE table_name<em><br />
        </em>(<br />
        column_name1 data_type,<em><br />
          </em>column_name2 data_type,<em><br />
            </em>column_name3 data_type,<em><br />
              &#8230;.</em><br />
        ) </td>
</tr>
</tbody>
</table>
<p>We must add the CREATE TABLE statement to the mysql_query() function   to   execute the command.</p>
<h3>Example</h3>
<p>The following example creates a table named &quot;Persons&quot;, with   three columns. The column names will be &quot;FirstName&quot;, &quot;LastName&quot; and   &quot;Age&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $con = mysql_connect(&quot;localhost&quot;,&quot;peter&quot;,&quot;abc123&quot;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        // Create database<br />
        if (mysql_query(&quot;CREATE DATABASE my_db&quot;,$con))<br />
        {<br />
        echo &quot;Database created&quot;;<br />
        }<br />
        else<br />
        {<br />
        echo &quot;Error creating database: &quot; . mysql_error();<br />
        }</p>
<p>        // Create table<br />
        mysql_select_db(&quot;my_db&quot;, $con);<br />
        $sql = &quot;CREATE TABLE Persons<br />
        (<br />
        FirstName varchar(15),<br />
        LastName varchar(15),<br />
        Age int<br />
        )&quot;;</p>
<p>        // Execute query<br />
        mysql_query($sql,$con);</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p><strong>Important:</strong> A database must be selected before a table can be   created.   The database is selected with the mysql_select_db() function.</p>
<p><strong>Note:</strong> When you create a database field of type varchar, you   must   specify the maximum length   of the field, e.g. varchar(15).</p>
<p>The data type specifies what type of data the column can hold. For a   complete   reference of all the data types available in MySQL, go to our complete <a href="http://www.w3schools.com/sql/sql_datatypes.asp">Data Types   reference</a>.
</p>
<hr />
<h2>Primary Keys and Auto Increment Fields</h2>
<p>Each table should have a    primary key field.</p>
<p>A primary key is used to uniquely identify the rows in a table. Each   primary   key value must be unique within the table. Furthermore, the primary key   field   cannot be null because the database engine requires a value to locate   the   record.</p>
<p>The following example sets the personID field as the primary key   field. The primary key field is often an ID number, and is often used   with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases   the value of the field   by 1 each time a new   record is added. To ensure that the primary key field cannot be null, we   must add the NOT NULL   setting to the field.</p>
<h3>Example</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> $sql = &quot;CREATE TABLE Persons <br />
        (<br />
        personID int NOT NULL AUTO_INCREMENT, <br />
        PRIMARY KEY(personID),<br />
        FirstName varchar(15),<br />
        LastName varchar(15),<br />
        Age int<br />
        )&quot;;</p>
<p>        mysql_query($sql,$con); </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-create-database-and-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP MySQL Introduction</title>
		<link>http://jaffnacampus.com/php/php-database/php-mysql-introduction/</link>
		<comments>http://jaffnacampus.com/php/php-database/php-mysql-introduction/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:23:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Database]]></category>
		<category><![CDATA[PHP MySQL Introduction]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=339</guid>
		<description><![CDATA[MySQL is the most popular open-source database system.

What is MySQL?
MySQL is a database.
The data in MySQL is stored in database [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL is the most popular open-source database system.</p>
<hr />
<h2>What is MySQL?</h2>
<p>MySQL is a database.</p>
<p>The data in MySQL is stored in database objects called tables.</p>
<p>A table is a collections of related data entries and it consists of   columns   and rows.</p>
<p>Databases are useful when storing information categorically. A   company may   have a database with the following tables: &quot;Employees&quot;, &quot;Products&quot;,   &quot;Customers&quot;   and &quot;Orders&quot;.</p>
<hr />
<h2>Database Tables</h2>
<p>A database most often contains one or more tables. Each table is   identified   by a name (e.g. &quot;Customers&quot; or &quot;Orders&quot;). Tables contain records (rows)   with   data.</p>
<p>Below is an example of a table called &quot;Persons&quot;:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">Address</th>
<th align="left">City</th>
</tr>
<tr>
<td>Hansen</td>
<td>Ola</td>
<td>Timoteivn 10</td>
<td>Sandnes</td>
</tr>
<tr>
<td>Svendson</td>
<td>Tove</td>
<td>Borgvn 23</td>
<td>Sandnes</td>
</tr>
<tr>
<td>Pettersen</td>
<td>Kari</td>
<td>Storgt 20</td>
<td>Stavanger</td>
</tr>
</tbody>
</table>
<p> The table above contains three records (one for each person) and four   columns (LastName, FirstName, Address, and City).</p>
<hr />
<h2> Queries</h2>
<p> A query is a question or a request.</p>
<p>With MySQL, we can query a database for specific information and have   a   recordset returned.</p>
<p>Look at the following query:</p>
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<tbody>
<tr>
<td> SELECT LastName FROM Persons </td>
</tr>
</tbody>
</table>
<p>The query above selects all the data in the &quot;LastName&quot; column from   the &quot;Persons&quot;   table, and will return a recordset like this: </p>
<table border="1" cellpadding="0" cellspacing="0" width="50%">
<tbody>
<tr>
<th align="left">LastName</th>
</tr>
<tr>
<td>Hansen</td>
</tr>
<tr>
<td>Svendson</td>
</tr>
<tr>
<td>Pettersen</td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Download MySQL Database</h2>
<p>If you don&#8217;t have a PHP server with a MySQL Database, you can   download MySQL   for free here: <a target="_blank" href="http://www.mysql.com/downloads/index.html"> http://www.mysql.com/downloads/index.html</a>
</p>
<hr />
<h2>Facts About MySQL Database</h2>
<p>One great thing about MySQL is that it can be scaled down to support   embedded   database applications. Perhaps it is because of this reputation that   many people   believe that MySQL can only handle small to medium-sized systems.</p>
<p>The truth is that MySQL is the de-facto standard database for web   sites that   support huge volumes of both data and end users (like Friendster, Yahoo,     Google).</p>
<p>Look at <a target="_blank" href="http://www.mysql.com/customers/"> http://www.mysql.com/customers/</a> for an overview of companies using   MySQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-database/php-mysql-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

