<?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>Mukesh Chapagain&#039;s Blog &#187; Database</title>
	<atom:link href="http://blog.chapagain.com.np/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chapagain.com.np</link>
	<description>PHP Magento jQuery SQL Wordpress Joomla Programming &#38; Tutorial</description>
	<lastBuildDate>Tue, 07 Feb 2012 00:54:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Alter MySQL table to add &amp; drop column &amp; add Foreign Key</title>
		<link>http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/</link>
		<comments>http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 00:54:41 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[alter table]]></category>
		<category><![CDATA[foreign key]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=1576</guid>
		<description><![CDATA[This article shows:- - How to add column to mysql database table after the table has already been created - How to delete column from mysql database table after the table has already been created - How to add foreign key to table column after the table has already been created Basically, all this can [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/' rel='bookmark' title='Permanent Link: 10 Very Useful SQL Queries with JOINS'>10 Very Useful SQL Queries with JOINS</a></li>
<li><a href='http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/' rel='bookmark' title='Permanent Link: Magento: Upgrading mysql setup of a module'>Magento: Upgrading mysql setup of a module</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This article shows:-</p>
<blockquote><p>
- How to add column to mysql database table after the table has already been created</p>
<p>- How to delete column from mysql database table after the table has already been created</p>
<p><span id="more-1576"></span></p>
<p>- How to add foreign key to table column after the table has already been created
</p></blockquote>
<p>Basically, all this can be done by &#8216;<strong>ALTER TABLE</strong>&#8216; statement. Here is an step-by-step tutorial on how we do it.</p>
<p><strong>Create parent table</strong><br />
<code><br />
CREATE TABLE IF NOT EXISTS parent(<br />
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,<br />
name VARCHAR(255) NOT NULL,<br />
description TEXT DEFAULT NULL,<br />
status TINYINT(1) NOT NULL,<br />
PRIMARY KEY(id)<br />
);<br />
</code></p>
<p><strong>Create child table</strong><br />
<code><br />
CREATE TABLE IF NOT EXISTS child(<br />
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,<br />
name VARCHAR(255) NOT NULL,<br />
description TEXT DEFAULT NULL,<br />
PRIMARY KEY(id)<br />
);<br />
</code></p>
<p>After we create both parent and child table, we remember that:- </p>
<blockquote><p>
- We have added an extra unnecessary column in parent table.<br />
- We have missed adding a column to child table.<br />
- And this column in child table should be the foreign key for the parent table.
</p></blockquote>
<p>So, our step would be:-</p>
<p><strong>First of all, delete the unncessary column from parent table</strong></p>
<p><code>ALTER TABLE parent DROP status;</code></p>
<p><strong>Secondly, add a new column in child table</strong></p>
<p><code>ALTER TABLE child ADD parent_id int(11);</code></p>
<p><strong>Finally, add foreign key contraint to the child table which relates to the parent table&#8217;s column (primary key)</strong></p>
<p><code>ALTER TABLE child ADD CONSTRAINT child_FK_1<br />
	FOREIGN KEY (parent_id) REFERENCES parent (id)<br />
	ON DELETE CASCADE;</code></p>
<p>More detail on MySQL foreign key and its implementation can be found on: <a href="http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/" target="_blank">MySQL Database: Foreign Key Understanding and Implementation</a></p>
<p>Thanks.</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=1576&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/' rel='bookmark' title='Permanent Link: 10 Very Useful SQL Queries with JOINS'>10 Very Useful SQL Queries with JOINS</a></li>
<li><a href='http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/' rel='bookmark' title='Permanent Link: Magento: Upgrading mysql setup of a module'>Magento: Upgrading mysql setup of a module</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Database: Foreign Key Understanding and Implementation</title>
		<link>http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/</link>
		<comments>http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 02:22:52 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[cascade]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=1262</guid>
		<description><![CDATA[Some definitions of foreign key:- A FOREIGN KEY in one table points to a PRIMARY KEY in another table. A foreign key is a field in a relational table that matches the primary key of another table. The foreign key can be used to cross-reference tables. A table may have multiple foreign keys, and each [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/' rel='bookmark' title='Permanent Link: 10 Very Useful SQL Queries with JOINS'>10 Very Useful SQL Queries with JOINS</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-delete-system-attribute/' rel='bookmark' title='Permanent Link: Magento: How to delete System Attribute?'>Magento: How to delete System Attribute?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Some definitions of foreign key:-</p>
<blockquote><p>A FOREIGN KEY in one table points to a PRIMARY KEY in another table.</p>
<p>A foreign key is a field in a relational table that matches the primary key of another table. The foreign key can be used to cross-reference tables. A table may have multiple foreign keys, and each foreign key can have a different referenced table.</p>
<p><span id="more-1262"></span></p>
<p>A Foreign Key is a Referential Constraint between two tables. A Referential Constraint or Referential Integrity is a property of data which, when satisfied, requires every value of one attribute (column) of a relation (table) to exist as a value of another attribute in a different (or the same) relation (table).
</p></blockquote>
<p>For MySQL, to support foreign key, the table type should be &#8216;<strong>InnoDB</strong>&#8216;. Therefore, when we are cross-referencing two tables through foreign key, both the tables must be InnoDB tables and they must not be Temporary tables.</p>
<p><strong>InnoDB </strong> also supports foreign key references within a table. The referencing and referenced table may be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known as a self-referencing or recursive foreign key.</p>
<p>When the user attempts to delete or update a row from a parent table, and there are one or more matching rows in the child table, InnoDB handles this through &#8220;<strong>Referential Action</strong>&#8220;. There are five options regarding the action to be taken. If <strong>ON DELETE</strong> or <strong>ON UPDATE</strong> are not specified, the default action is <strong>RESTRICT</strong>.</p>
<blockquote><p>
<strong>CASCADE</strong>: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. </p>
<p><strong>SET NULL</strong>: Delete or update the row from the parent table and set the foreign key column or columns in the child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.</p>
<p><strong>NO ACTION</strong>: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not permitted to proceed if there is a related foreign key value in the referenced table. InnoDB rejects the delete or update operation for the parent table.</p>
<p><strong>RESTRICT</strong>: Rejects the delete or update operation for the parent table. Specifying RESTRICT (or NO ACTION) is the same as omitting the ON DELETE or ON UPDATE clause. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. However, in MySQL, foreign key constraints are checked immediately, so NO ACTION is the same as RESTRICT.</p>
<p><strong>SET DEFAULT</strong>: Similar to SET NULL, the foreign key values in the referencing row are set to the column default when the referenced row is updated or deleted. This action is recognized by the parser, but InnoDB rejects table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.
</p></blockquote>
<p>Here, I will be implementing the foreign key <strong>CASCADE </strong>update and delete.</p>
<p><strong>CASCADE</strong>:-</p>
<p>Whenever rows in the master (referenced) table are deleted, the respective rows of the child (referencing) table with a matching foreign key column will get deleted as well. This is called a cascade delete.</p>
<p>Example Tables: test_customer(customer_id, name, address) and test_order(order_id, customer_id, status)</p>
<p>test_customer is the master table and test_order is the child table, where &#8216;customer_id&#8217; is the foreign key in test_order and represents the customer who placed the order. When a row of test_customer is deleted, any test_order row matching the deleted test_customer&#8217;s customer_id will also be deleted.</p>
<p><strong>Create Tables</strong></p>
<p><code>CREATE TABLE test_customer (<br />
    customer_id int(10) unsigned NOT NULL auto_increment,<br />
    name varchar(100),<br />
    address varchar(100),<br />
    PRIMARY KEY (customer_id)<br />
) ENGINE=InnoDB;</p>
<p>CREATE TABLE test_order (<br />
    order_id int(10) unsigned NOT NULL auto_increment,<br />
    customer_id int(10) unsigned NOT NULL,<br />
    status varchar(30),<br />
    PRIMARY KEY (order_id)<br />
) ENGINE=InnoDB; </code></p>
<p><strong>Add Foreign Key</strong></p>
<p><code>ALTER TABLE test_order<br />
ADD CONSTRAINT FK_test_order<br />
FOREIGN KEY (customer_id) REFERENCES test_customer(customer_id)<br />
ON UPDATE CASCADE<br />
ON DELETE CASCADE; </code></p>
<p><strong>Alternative way</strong></p>
<p>Alternatively we can directly add Foreign Key at the time of table creation. You may choose any one way (the following way or the way above) for creating tables and assigning foreign key.</p>
<p><code>SET foreign_key_checks = 0;</p>
<p>CREATE TABLE test_customer (<br />
    customer_id int(10) unsigned NOT NULL auto_increment,<br />
    name varchar(100),<br />
    address varchar(100),<br />
    PRIMARY KEY (customer_id)<br />
) ENGINE=InnoDB;</p>
<p>CREATE TABLE test_order (<br />
    order_id int(10) unsigned NOT NULL auto_increment,<br />
    customer_id int(10) unsigned NOT NULL,<br />
    status varchar(30),<br />
    PRIMARY KEY (order_id),<br />
	CONSTRAINT FK_test_order<br />
	FOREIGN KEY (customer_id) REFERENCES test_customer(customer_id)<br />
	ON UPDATE CASCADE ON DELETE CASCADE<br />
) ENGINE=InnoDB; </p>
<p>SET foreign_key_checks = 1;</code></p>
<p><strong>Insert sample data to test_customer and test_order tables</strong></p>
<p><code>INSERT INTO  test_customer<br />
(  customer_id ,  name ,  address )<br />
VALUES<br />
( '1',  'John',  'New York' ),<br />
( '2',  'Tom',  'London' );</p>
<p>INSERT INTO  test_order<br />
(  order_id ,  customer_id ,  status )<br />
VALUES<br />
( '1',  '1',  'pending' ),<br />
( '2',  '1',  'processing' ),<br />
( '3',  '2',  'complete' ),<br />
( '4',  '2',  'pending' );</code></p>
<p><strong>Doing UPDATE CASCADE</strong></p>
<p>When we update customer_id in test_customer (parent table), the customer_id field in test_order (child table) will also get updated.</p>
<p><code>UPDATE test_customer SET customer_id = 3 WHERE customer_id = 1;</code></p>
<p><strong>Note</strong>:- </p>
<p>We <strong>CANNOT </strong>directly update the foreign key in child table. </p>
<p>Doing this will result in error saying &#8220;<em>Cannot add or update a child row: a foreign key constraint fails</em>&#8220;.</p>
<p><code>UPDATE test_order SET customer_id = 4 WHERE customer_id = 3;</code></p>
<p><strong>Doing DELETE CASCADE</strong></p>
<p>When we delete customer_id in test_customer (parent table), the customer_id field in test_order (child table) will also get deleted.</p>
<p><code>DELETE FROM test_customer WHERE customer_id = 2;</code></p>
<p><strong>Note</strong>:-</p>
<p>We <strong>CAN </strong>directly delete rows from test_order (child table) like below:-</p>
<p><code>DELETE FROM test_order WHERE customer_id = 3;</code></p>
<p>Hope this helps. Thanks</p>
<p><strong>References</strong>:-<br />
1. <a target="_blank" rel="nofollow" href="http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html">http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html</a><br />
2. <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Foreign_key">http://en.wikipedia.org/wiki/Foreign_key</a></p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=1262&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/' rel='bookmark' title='Permanent Link: 10 Very Useful SQL Queries with JOINS'>10 Very Useful SQL Queries with JOINS</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-delete-system-attribute/' rel='bookmark' title='Permanent Link: Magento: How to delete System Attribute?'>Magento: How to delete System Attribute?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Magento: Join, filter, select and sort attributes, fields and tables</title>
		<link>http://blog.chapagain.com.np/magento-join-filter-select-and-sort-attributes-fields-and-tables/</link>
		<comments>http://blog.chapagain.com.np/magento-join-filter-select-and-sort-attributes-fields-and-tables/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 14:34:06 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=981</guid>
		<description><![CDATA[In my previous article (Magento: Very Useful Collection Functions), I had written about database interaction functions present in class Varien_Data_Collection_Db. Here, I am going to explain some database interaction functions present in the class Mage_Eav_Model_Entity_Collection_Abstract. These collection functions are very useful to select data from Magento database. We need them almost all the time for [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/' rel='bookmark' title='Permanent Link: Magento: How to filter product collection using 2 or more category filters?'>Magento: How to filter product collection using 2 or more category filters?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-collection-functions/' rel='bookmark' title='Permanent Link: Magento: Very Useful Collection Functions'>Magento: Very Useful Collection Functions</a></li>
<li><a href='http://blog.chapagain.com.np/magento-adding-category-attributes/' rel='bookmark' title='Permanent Link: Magento: Adding category attributes'>Magento: Adding category attributes</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-search-or-filter-by-multiselect-attribute-in-admin-grid/' rel='bookmark' title='Permanent Link: Magento: How to search or filter by multiselect attribute in admin grid?'>Magento: How to search or filter by multiselect attribute in admin grid?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-sort-latest-product-by-created-date-and-new-from-date/' rel='bookmark' title='Permanent Link: Magento: Sort latest product by &#8216;created date&#8217; and &#8216;new from date&#8217;'>Magento: Sort latest product by &#8216;created date&#8217; and &#8216;new from date&#8217;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In my previous article (<strong><a href="http://blog.chapagain.com.np/magento-collection-functions/">Magento: Very Useful Collection Functions</a></strong>), I had written about database interaction functions present in class <strong>Varien_Data_Collection_Db</strong>.</p>
<p>Here, I am going to explain some database interaction functions present in the class <strong>Mage_Eav_Model_Entity_Collection_Abstract</strong>. These collection functions are very useful to select data from Magento database. We need them almost all the time for filtering collection object.</p>
<p><span id="more-981"></span></p>
<p>Below are some of the useful functions that we use most often.</p>
<p><strong>Class: Mage_Eav_Model_Entity_Collection_Abstract</strong></p>
<p><strong>addAttributeToFilter</strong>: adds WHERE clause on $attribute specified by $condition</p>
<blockquote><p>
/**<br />
* Add attribute filter to collection<br />
*<br />
* If $attribute is an array will add OR condition with following format:<br />
* array(<br />
*     array(&#8216;attribute&#8217;=&gt;&#8217;firstname&#8217;, &#8216;like&#8217;=&gt;&#8217;test%&#8217;),<br />
*     array(&#8216;attribute&#8217;=&gt;&#8217;lastname&#8217;, &#8216;like&#8217;=&gt;&#8217;test%&#8217;),<br />
* )<br />
*<br />
* @see self::_getConditionSql for $condition<br />
* @param Mage_Eav_Model_Entity_Attribute_Interface|integer|string|array $attribute<br />
* @param null|string|array $condition<br />
* @param string $operator<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
addAttributeToFilter($attribute, $condition=null, $joinType=&#8217;inner&#8217;)
</p></blockquote>
<p><strong>addAttributeToSelect</strong>: gets the value for $attribute in the SELECT clause; specify * to get all attributes (i.e. to execute SELECT *)</p>
<blockquote><p>
/**<br />
* Add attribute to entities in collection<br />
*<br />
* If $attribute==&#8217;*&#8217; select all attributes<br />
*<br />
* @param   array|string|integer|Mage_Core_Model_Config_Element $attribute<br />
* @param   false|string $joinType flag for joining attribute<br />
* @return  Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
addAttributeToSelect($attribute, $joinType=false)
</p></blockquote>
<p>If an array is passed but no attribute code specified, it will be interpreted as a group of OR conditions that will be processed in the same way.<br />
If no attribute code is specified, it defaults to eq.</p>
<pre class="brush: php; title: ; notranslate">
$collection = Mage::getModel('catalog/product')-&gt;getCollection();

// select all attributes
$collection-&gt;addAttributeToSelect('*');

// select specific attributes
$collection-&gt;addAttributeToSelect(array('name', 'url_key', 'type_id'));

// select only those items whose status = 1
$collection-&gt;addAttributeToFilter('status', 1);

// alternative to select only those items whose status = 1
$collection-&gt;addAttributeToFilter('status', array('eq' =&gt; 1));

// using LIKE statement
$collection-&gt;addAttributeToFilter('sku', array('like' =&gt; '%CH%'));

// using IN statement,
// i.e. selecting only those items whose ID fall in the given array
$collection-&gt;addAttributeToFilter('id', array('in' =&gt; array(1, 14, 51, 52)));

// selecting only those items whose ID is greater than the given value
$collection-&gt;addAttributeToFilter('id', array('gt' =&gt; 5));

// select by date range
$collection-&gt;addAttributeToFilter('date_field', array(
    'from' =&gt; '10 September 2010',
    'from' =&gt; '21 September 2010',
    'date' =&gt; true, // specifies conversion of comparison values
    ));

// Add OR condition:
$collection-&gt;addAttributeToFilter(array(
    array(
        'attribute' =&gt; 'field_name',
        'in'        =&gt; array(1, 2, 3),
        ),
    array(
        'attribute' =&gt; 'date_field',
        'from'      =&gt; '2010-09-10',
        ),
    ));
</pre>
<p><strong>Below is the full filter condition codes with attribute code and its sql equivalent</strong></p>
<blockquote><p>
eq	:	=<br />
neq	:	!=<br />
like :	LIKE<br />
nlike :	NOT LIKE<br />
in	:	IN ()<br />
nin	:	NOT IN ()<br />
is	:	IS<br />
notnull :	IS NOT NULL<br />
null :	IS NULL<br />
moreq :	>=<br />
gt	:	><br />
lt	:	<<br />
gteq :	>=<br />
lteq :	<=<br />
finset :	FIND_IN_SET()<br />
from :	>=	 (for use with dates)<br />
to	:	<=	 (for use with dates)<br />
date :	optional flag for use with from/to to specify that comparison value should first be converted to a date<br />
datetime :	optional flag for use with from/to to specify that comparison value should first be converted to a datetime
</p></blockquote>
<p><strong>addFieldToFilter</strong>: alias for addAttributeToFilter(). This filters the database table fields.</p>
<blockquote><p>
/**<br />
* Wrapper for compatibility with Varien_Data_Collection_Db<br />
*<br />
* @param mixed $attribute<br />
* @param mixed $condition<br />
*/<br />
addFieldToFilter($attribute, $condition=null)
</p></blockquote>
<p><strong>addAttributeToSort</strong>: adds ORDER BY clause on $attribute</p>
<blockquote><p>
/**<br />
* Add attribute to sort order<br />
*<br />
* @param string $attribute<br />
* @param string $dir<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
addAttributeToSort($attribute, $dir=&#8217;asc&#8217;)
</p></blockquote>
<p><strong>addExpressionAttributeToSelect</strong>: adds SQL expression $expression, using $alias, to SELECT clause (typically containing aggregate functions such as SUM(), COUNT()); when $attribute specifies a single attribute as a string, $expression can reference the attribute as simply {{attribute}}, but when passing an array of attributes, each attribute must be referenced in $expression by the name of the specific attribute;</p>
<blockquote><p>
/**<br />
* Add attribute expression (SUM, COUNT, etc)<br />
*<br />
* Example: (&#8216;sub_total&#8217;, &#8216;SUM({{attribute}})&#8217;, &#8216;revenue&#8217;)<br />
* Example: (&#8216;sub_total&#8217;, &#8216;SUM({{revenue}})&#8217;, &#8216;revenue&#8217;)<br />
*<br />
* For some functions like SUM use groupByAttribute.<br />
*<br />
* @param string $alias<br />
* @param string $expression<br />
* @param string $attribute<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
addExpressionAttributeToSelect($alias, $expression, $attribute)
</p></blockquote>
<p><strong>groupByAttribute</strong>: adds $attribute to GROUP BY clause</p>
<blockquote><p>
/**<br />
* Groups results by specified attribute<br />
*<br />
* @param string|array $attribute<br />
*/<br />
groupByAttribute($attribute)
</p></blockquote>
<p><strong>joinAttribute</strong>: joins another entity and adds attribute from joined entity, using $alias, to SELECT clause.</p>
<p>Here are the parameters for joinAttribute function:-</p>
<p><strong>$alias</strong> = selected field name. You can keep it&#8217;s name whatever you want.</p>
<p><strong>$attribute</strong> = joined entity type code and attribute code = <strong>entity_type_code/attribute_code</strong><br />
<strong>entity_type_code</strong> is present in <strong>eav_entity_type table</strong><br />
<strong>attribute_code</strong> is present in <strong>eav_attribute table</strong><br />
attribute_code is attribute of the corresponding entity you want to select out.</p>
<p><strong>$bind</strong> = attribute code of the main entity to link to the joined entity.</p>
<p><strong>$filter</strong> = primary key for the joined entity (entity_id default)</p>
<blockquote><p>
/**<br />
* Add attribute from joined entity to select<br />
*<br />
* Examples:<br />
* (&#8216;billing_firstname&#8217;, &#8216;customer_address/firstname&#8217;, &#8216;default_billing&#8217;)<br />
* (&#8216;billing_lastname&#8217;, &#8216;customer_address/lastname&#8217;, &#8216;default_billing&#8217;)<br />
* (&#8216;shipping_lastname&#8217;, &#8216;customer_address/lastname&#8217;, &#8216;default_billing&#8217;)<br />
* (&#8216;shipping_postalcode&#8217;, &#8216;customer_address/postalcode&#8217;, &#8216;default_shipping&#8217;)<br />
* (&#8216;shipping_city&#8217;, $cityAttribute, &#8216;default_shipping&#8217;)<br />
*<br />
* Developer is encouraged to use existing instances of attributes and entities<br />
* After first use of string entity name it will be cached in the collection<br />
*<br />
* @todo connect between joined attributes of same entity<br />
* @param string $alias alias for the joined attribute<br />
* @param string|Mage_Eav_Model_Entity_Attribute_Abstract $attribute<br />
* @param string $bind attribute of the main entity to link with joined $filter<br />
* @param string $filter primary key for the joined entity (entity_id default)<br />
* @param string $joinType inner|left<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
joinAttribute($alias, $attribute, $bind, $filter=null, $joinType=&#8217;inner&#8217;, $storeId=null)
</p></blockquote>
<p><strong>joinTable</strong>: joins table $table</p>
<p>Here are the parameters of the function joinTable:-</p>
<p>$table = table name to join<br />
$bind = ( parent_key = foreign_key )<br />
$fields = array of fields to select<br />
$cond = where condition<br />
$joinType = join type</p>
<blockquote><p>
/**<br />
* Join a table<br />
*<br />
* @param string|array $table<br />
* @param string $bind<br />
* @param string|array $fields<br />
* @param null|array $cond<br />
* @param string $joinType<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
joinTable($table, $bind, $fields=null, $cond=null, $joinType=&#8217;inner&#8217;)
</p></blockquote>
<p><strong>Using joinAttribute and joinTable</strong></p>
<p>In the code below, all order invoice items are selected, i.e. all products that have been invoiced.<br />
joinTable is used to join sales_order_entity table to fetch increment_id and store_id of the invoice for each product.<br />
joinAttribute is used to fetch order_id, product_name, and store_id.<br />
joinTable is used again to fetch the order status of each invoice item.</p>
<pre class="brush: php; title: ; notranslate">
$collection = Mage::getModel('sales/order_invoice_item')
					-&gt;getCollection()
					-&gt;joinTable('sales_order_entity', 'entity_id=parent_id', array('invoice_id'=&gt;'increment_id', 'store_id' =&gt; 'store_id'), null , 'left')
					-&gt;joinAttribute('order_id', 'invoice/order_id', 'parent_id', null, 'left')
					-&gt;joinAttribute('product_name', 'invoice_item/name', 'entity_id', null, 'left')
					-&gt;joinAttribute('store_id', 'invoice/store_id', 'parent_id', null, 'left')

					-&gt;joinTable('sales_order', 'entity_id=order_id', array('order_status'=&gt;'status'), null , 'left')
					;
</pre>
<p><strong>joinField</strong>: joins regular table field using an attribute as foreign key</p>
<blockquote><p>
/**<br />
* Join regular table field and use an attribute as fk<br />
*<br />
* Examples:<br />
* (&#8216;country_name&#8217;, &#8216;directory/country_name&#8217;, &#8216;name&#8217;, &#8216;country_id=shipping_country&#8217;, &#8220;{{table}}.language_code=&#8217;en&#8217;&#8221;, &#8216;left&#8217;)<br />
*<br />
* @param string $alias &#8216;country_name&#8217;<br />
* @param string $table &#8216;directory/country_name&#8217;<br />
* @param string $field &#8216;name&#8217;<br />
* @param string $bind &#8216;PK(country_id)=FK(shipping_country_id)&#8217;<br />
* @param string|array $cond &#8220;{{table}}.language_code=&#8217;en&#8217;&#8221; OR array(&#8216;language_code&#8217;=&gt;&#8217;en&#8217;)<br />
* @param string $joinType &#8216;left&#8217;<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
joinField($alias, $table, $field, $bind, $cond=null, $joinType=&#8217;inner&#8217;)
</p></blockquote>
<p><strong>removeAttributeToSelect</strong>: removes $attribute from SELECT clause; specify null to remove all attributes</p>
<blockquote><p>
/**<br />
* Remove an attribute from selection list<br />
*<br />
* @param string $attribute<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
removeAttributeToSelect($attribute=null)
</p></blockquote>
<p><strong>setPage</strong>: sets LIMIT clause by specifying page number (one-indexed) and number of records per page; equivalent to calling setCurPage($pageNum) and setPageSize($pageSize)</p>
<blockquote><p>
/**<br />
* Set collection page start and records to show<br />
*<br />
* @param integer $pageNum<br />
* @param integer $pageSize<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
setPage($pageNum, $pageSize)
</p></blockquote>
<p><strong>importFromArray</strong>: imports 2D array into collection as objects</p>
<blockquote><p>
/**<br />
* Import 2D array into collection as objects<br />
*<br />
* If the imported items already exist, update the data for existing objects<br />
*<br />
* @param array $arr<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
importFromArray($arr)
</p></blockquote>
<p><strong>exportToArray</strong>: returns collection data as a 2D array</p>
<blockquote><p>
/**<br />
* Get collection data as a 2D array<br />
*<br />
* @return array<br />
*/<br />
exportToArray()
</p></blockquote>
<p><strong>setOrder</strong>: alias for addAttributeToSort() q.v., identical except that it can accept array of attributes, and default $dir is desc</p>
<blockquote><p>
/**<br />
* Set sorting order<br />
*<br />
* $attribute can also be an array of attributes<br />
*<br />
* @param string|array $attribute<br />
* @param string $dir<br />
* @return Mage_Eav_Model_Entity_Collection_Abstract<br />
*/<br />
setOrder($attribute, $dir=&#8217;desc&#8217;)
</p></blockquote>
<blockquote><p>Inspired by Magento Wiki:<br />
<a href="http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/using_collections_in_magento" rel="nofollow" target="_blank">http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/using_collections_in_magento</a>.<br />
However, more of my code, text &#038; understanding are added in this article.
</p></blockquote>
<p>Hope this helps. Thanks.</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=981&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/' rel='bookmark' title='Permanent Link: Magento: How to filter product collection using 2 or more category filters?'>Magento: How to filter product collection using 2 or more category filters?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-collection-functions/' rel='bookmark' title='Permanent Link: Magento: Very Useful Collection Functions'>Magento: Very Useful Collection Functions</a></li>
<li><a href='http://blog.chapagain.com.np/magento-adding-category-attributes/' rel='bookmark' title='Permanent Link: Magento: Adding category attributes'>Magento: Adding category attributes</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-search-or-filter-by-multiselect-attribute-in-admin-grid/' rel='bookmark' title='Permanent Link: Magento: How to search or filter by multiselect attribute in admin grid?'>Magento: How to search or filter by multiselect attribute in admin grid?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-sort-latest-product-by-created-date-and-new-from-date/' rel='bookmark' title='Permanent Link: Magento: Sort latest product by &#8216;created date&#8217; and &#8216;new from date&#8217;'>Magento: Sort latest product by &#8216;created date&#8217; and &#8216;new from date&#8217;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/magento-join-filter-select-and-sort-attributes-fields-and-tables/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Magento Error – Notice: Undefined index: 0 app/code/core/Mage/Core/Model/Mysql4/Config.php on line 92</title>
		<link>http://blog.chapagain.com.np/magento-error-%e2%80%93-notice-undefined-index-0-appcodecoremagecoremodelmysql4config-php-on-line-92/</link>
		<comments>http://blog.chapagain.com.np/magento-error-%e2%80%93-notice-undefined-index-0-appcodecoremagecoremodelmysql4config-php-on-line-92/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 11:15:19 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=886</guid>
		<description><![CDATA[I got this error message when migrating my Magento files and database from one server to another. Notice: Undefined index: 0 in app/code/core/Mage/Core/Model/Mysql4/Config.php on line 92 Solution: - Open PhpMyAdmin - Go to your database - Click SQL - Run the following SQL Query: That&#8217;s all. Your error is solved now. Hope this helps. Thanks. [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-recoverable-error-argument-1-passed-to-mage_core_model_store-setwebsite-must-be-an-instance-of-mage_core_model_website/' rel='bookmark' title='Permanent Link: Magento: Recoverable Error: Argument 1 passed to Mage_Core_Model_Store :: setWebsite() must be an instance of Mage_Core_Model_Website'>Magento: Recoverable Error: Argument 1 passed to Mage_Core_Model_Store :: setWebsite() must be an instance of Mage_Core_Model_Website</a></li>
<li><a href='http://blog.chapagain.com.np/magento-solution-to-error-404-not-found-in-admin-login-page/' rel='bookmark' title='Permanent Link: Magento: Solution to &#8220;Error: 404 Not Found&#8221; in Admin Login Page'>Magento: Solution to &#8220;Error: 404 Not Found&#8221; in Admin Login Page</a></li>
<li><a href='http://blog.chapagain.com.np/magento-fatal-error-call-to-a-member-function-gettable-on-a-non-object/' rel='bookmark' title='Permanent Link: Magento: Fatal error: Call to a member function getTable() on a non-object'>Magento: Fatal error: Call to a member function getTable() on a non-object</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-remove-index-php-from-url/' rel='bookmark' title='Permanent Link: Magento: How to remove index.php from URL?'>Magento: How to remove index.php from URL?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-change-currency-symbol-by-model-override/' rel='bookmark' title='Permanent Link: Magento: How to change Currency symbol by Model Override ?'>Magento: How to change Currency symbol by Model Override ?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I got this error message when migrating my Magento files and database from one server to another. </p>
<blockquote><p>Notice: Undefined index: 0 in app/code/core/Mage/Core/Model/Mysql4/Config.php on line 92</p></blockquote>
<p><strong>Solution:</strong></p>
<p><span id="more-886"></span></p>
<p>- Open PhpMyAdmin<br />
- Go to your database<br />
- Click SQL<br />
- Run the following SQL Query:</p>
<pre class="brush: php; title: ; notranslate">
SET FOREIGN_KEY_CHECKS=0;
UPDATE `core_store` SET store_id = 0 WHERE code='admin';
UPDATE `core_store_group` SET group_id = 0 WHERE name='Default';
UPDATE `core_website` SET website_id = 0 WHERE code='admin';
UPDATE `customer_group` SET customer_group_id = 0 WHERE customer_group_code='NOT LOGGED IN';
SET FOREIGN_KEY_CHECKS=1;
</pre>
<p>That&#8217;s all. Your error is solved now. </p>
<p>Hope this helps. Thanks.</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=886&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-recoverable-error-argument-1-passed-to-mage_core_model_store-setwebsite-must-be-an-instance-of-mage_core_model_website/' rel='bookmark' title='Permanent Link: Magento: Recoverable Error: Argument 1 passed to Mage_Core_Model_Store :: setWebsite() must be an instance of Mage_Core_Model_Website'>Magento: Recoverable Error: Argument 1 passed to Mage_Core_Model_Store :: setWebsite() must be an instance of Mage_Core_Model_Website</a></li>
<li><a href='http://blog.chapagain.com.np/magento-solution-to-error-404-not-found-in-admin-login-page/' rel='bookmark' title='Permanent Link: Magento: Solution to &#8220;Error: 404 Not Found&#8221; in Admin Login Page'>Magento: Solution to &#8220;Error: 404 Not Found&#8221; in Admin Login Page</a></li>
<li><a href='http://blog.chapagain.com.np/magento-fatal-error-call-to-a-member-function-gettable-on-a-non-object/' rel='bookmark' title='Permanent Link: Magento: Fatal error: Call to a member function getTable() on a non-object'>Magento: Fatal error: Call to a member function getTable() on a non-object</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-remove-index-php-from-url/' rel='bookmark' title='Permanent Link: Magento: How to remove index.php from URL?'>Magento: How to remove index.php from URL?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-change-currency-symbol-by-model-override/' rel='bookmark' title='Permanent Link: Magento: How to change Currency symbol by Model Override ?'>Magento: How to change Currency symbol by Model Override ?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/magento-error-%e2%80%93-notice-undefined-index-0-appcodecoremagecoremodelmysql4config-php-on-line-92/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>10 Very Useful SQL Queries with JOINS</title>
		<link>http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/</link>
		<comments>http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/#comments</comments>
		<pubDate>Fri, 14 May 2010 19:30:10 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=779</guid>
		<description><![CDATA[The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. [@via w3schools] A SQL JOIN clause combines records from two or more tables in a database. It creates a set that can be saved as a table or [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-join-filter-select-and-sort-attributes-fields-and-tables/' rel='bookmark' title='Permanent Link: Magento: Join, filter, select and sort attributes, fields and tables'>Magento: Join, filter, select and sort attributes, fields and tables</a></li>
<li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/fun-with-strings-in-php-part-1/' rel='bookmark' title='Permanent Link: Fun with strings in PHP (Part 1)'>Fun with strings in PHP (Part 1)</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. [@via w3schools]</p></blockquote>
<blockquote><p>A SQL JOIN clause combines records from two or more tables in a database. It creates a set that can be saved as a table or used as is. A JOIN is a means for combining fields from two tables by using values common to each. ANSI standard SQL specifies four types of JOINs: INNER, OUTER, LEFT, and RIGHT. In special cases, a table (base table, view, or joined table) can JOIN to itself in a self-join. [@via wikipedia]</p></blockquote>
<p><span id="more-779"></span></p>
<p>SQL Joins are helpful when we have to fetch data with a single query from two or more database tables.</p>
<p><strong>Case:</strong></p>
<p>I need to store news/article information. The news can also have comments and tags. News can be submitted/posted by multiple users.</p>
<p>According to the above scenario, I have created 5 tables and inserted some data into them :-</p>
<p><strong>news </strong>- to store news<br />
<strong>comments </strong>- to store comments for any particular news<br />
<strong>tags </strong>- to store tags associated with any particular news<br />
<strong>users </strong>- to store user information<br />
<strong>tags_news</strong> &#8211; to store relationship between news and tags</p>
<p>&#8211;<br />
&#8211; <strong>Table structure for table `comments`</strong><br />
&#8211;<br />
<code><br />
CREATE TABLE `comments` (<br />
  `id` bigint(20) NOT NULL auto_increment,<br />
  `news_id` bigint(20) NOT NULL,<br />
  `user_id` bigint(20) NOT NULL,<br />
  `detail` text NOT NULL,<br />
  `status` tinyint(1) NOT NULL default '1',<br />
  `comment_date` int(11) NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  KEY `user_id` (`user_id`),<br />
  KEY `news_id` (`news_id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br />
</code></p>
<p>&#8211;<br />
&#8211; <strong>Dumping data for table `comments`</strong><br />
&#8211;<br />
<code><br />
INSERT INTO `comments` (`id`, `news_id`, `user_id`, `detail`, `status`, `comment_date`) VALUES<br />
(1, 1, 2, 'nice post :)', 1, 1273431296),<br />
(2, 2, 2, 'hahaha', 1, 1273431420),<br />
(3, 3, 2, '3123123', 1, 1273431452),<br />
(4, 1, 2, 'thank You', 1, 1273431475),<br />
(5, 2, 1, 'congratulations!', 1, 1273431500);<br />
</code><br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>&#8211;<br />
&#8211; <strong>Table structure for table `news`</strong><br />
&#8211;<br />
<code><br />
CREATE TABLE `news` (<br />
  `id` bigint(20) NOT NULL auto_increment,<br />
  `user_id` bigint(20) NOT NULL,<br />
  `title` text NOT NULL,<br />
  `detail` text NOT NULL,<br />
  `visit` int(11) NOT NULL default '0',<br />
  `status` tinyint(1) NOT NULL default '1',<br />
  `created_date` int(11) NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  KEY `user_id` (`user_id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br />
</code><br />
&#8211;<br />
&#8211; <strong>Dumping data for table `news`</strong><br />
&#8211;<br />
<code><br />
INSERT INTO `news` (`id`, `user_id`, `title`, `detail`, `visit`, `status`, `created_date`) VALUES<br />
(1, 1, 'Extra Content', 'fasdfasdf', 2, 1, 1273431296),<br />
(2, 1, 'My My Question', 'this is jpt question.. :D', 16, 1, 1273431389),<br />
(3, 2, 'Am I ram?', 'I am ram..yahoo !!', 3, 1, 1273431420);<br />
</code><br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>&#8211;<br />
&#8211; <strong>Table structure for table `tags`</strong><br />
&#8211;<br />
<code><br />
CREATE TABLE `tags` (<br />
  `id` bigint(20) NOT NULL auto_increment,<br />
  `name` varchar(255) NOT NULL,<br />
  `slug` varchar(255) NOT NULL,<br />
  PRIMARY KEY  (`id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br />
</code><br />
&#8211;<br />
&#8211; <strong>Dumping data for table `tags`</strong><br />
&#8211;<br />
<code><br />
INSERT INTO `tags` (`id`, `name`, `slug`) VALUES<br />
(1, 'sagarmatha', 'sagarmatha'),<br />
(2, 'nepal', 'nepal'),<br />
(3, 'gautam buddha', 'gautam-buddha'),<br />
(4, 'testing', 'testing'),<br />
(5, 'tags', 'tags'),<br />
(6, 'tasty apple', 'tasty-apple'),<br />
(7, 'banana', 'banana');<br />
</code><br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>&#8211;<br />
&#8211; <strong>Table structure for table `tags_news`</strong><br />
&#8211;<br />
<code><br />
CREATE TABLE `tags_news` (<br />
  `id` bigint(20) NOT NULL auto_increment,<br />
  `news_id` bigint(20) NOT NULL,<br />
  `tags_id` bigint(20) NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  KEY `news_id` (`news_id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br />
</code><br />
&#8211;<br />
&#8211; <strong>Dumping data for table `tags_news`</strong><br />
&#8211;<br />
<code><br />
INSERT INTO `tags_news` (`id`, `news_id`, `tags_id`) VALUES<br />
(1, 1, 1),<br />
(2, 1, 2),<br />
(3, 1, 3),<br />
(4, 2, 4),<br />
(5, 2, 5),<br />
(6, 2, 6),<br />
(7, 3, 6),<br />
(8, 3, 7);<br />
</code><br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>&#8211;<br />
&#8211; <strong>Table structure for table `users`</strong><br />
&#8211;<br />
<code><br />
CREATE TABLE `users` (<br />
  `id` bigint(20) NOT NULL auto_increment,<br />
  `user_id` bigint(20) NOT NULL,<br />
  `firstname` varchar(100) NOT NULL,<br />
  `lastname` varchar(100) NOT NULL,<br />
  PRIMARY KEY  (`id`),<br />
  KEY `user_id` (`user_id`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;<br />
</code><br />
&#8211;<br />
&#8211; <strong>Dumping data for table `users`</strong><br />
&#8211;<br />
<code><br />
INSERT INTO `users` (`id`, `user_id`, `firstname`, `lastname`) VALUES<br />
(1, 1, 'Mukesh', 'Chapagain'),<br />
(2, 2, 'Christopher', 'Gayle'),<br />
(3, 3, 'Brian', 'Lara');<br />
</code><br />
&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Now, by using SQL JOINS, I can fetch data from these tables for different conditions and in different ways. Here follows the different SQL Queries with the magical JOIN statements:-</p>
<p><strong>1) SELECT NEWS WITH COMMENTS COUNT FOR EACH NEWS</strong></p>
<p><code><br />
SELECT n.*, ifnull(c.count,0) AS comment FROM news AS n<br />
	LEFT JOIN<br />
		(SELECT COUNT(comments.id) AS count, comments.news_id FROM comments GROUP BY comments.news_id) AS c<br />
			ON n.id = c.news_id<br />
				ORDER BY n.created_date DESC;<br />
</code>		</p>
<p><strong>2) SELECT NEWS POSTED BY ANY PARTICULAR USER (HERE, user_id = 1)</strong></p>
<p><code><br />
SELECT n.* FROM news AS n<br />
	INNER JOIN<br />
		users AS u<br />
			ON n.user_id = u.id<br />
				WHERE u.id = 1;<br />
</code></p>
<p><strong>3) SELECT NEWS POSTED BY ANY PARTICULAR USER (WITH USER&#8217;S FIRSTNAME AND LASTNAME) (HERE, user_id = 1)</strong></p>
<p><code><br />
SELECT n.*, u.firstname AS firstname, u.lastname AS lastname FROM news AS n<br />
	INNER JOIN<br />
		users AS u<br />
			ON n.user_id = u.id<br />
				WHERE u.id = 1;<br />
</code>			</p>
<p><strong>4) SELECT NEWS POSTED BY ANY PARTICULAR USER (WITH COMMENTS COUNT FOR EACH NEWS) (HERE, user_id = 1)</strong></p>
<p><code><br />
SELECT n.*, ifnull(c.count,0) AS comment, u.firstname AS firstname, u.lastname AS lastname FROM news AS n<br />
	LEFT JOIN<br />
		(SELECT COUNT(comments.id) AS count, comments.news_id FROM comments GROUP BY comments.news_id) AS c<br />
			ON n.id = c.news_id<br />
				INNER JOIN<br />
					users AS u<br />
						ON n.user_id = u.id<br />
							WHERE u.id = 1;<br />
</code></p>
<p><strong>5) SELECT COMMENTS FOR ANY PARTICULAR NEWS (HERE, news_id =1)</strong></p>
<p><code><br />
SELECT c.* FROM comments AS c<br />
	INNER JOIN<br />
		news AS n<br />
			ON c.news_id = n.id<br />
				WHERE c.news_id = 1;<br />
</code></p>
<p><strong>6) SELECT COMMENTS FOR ANY PARTICULAR NEWS (ALONG WITH USER INFORMATION) (HERE, news_id = 1)</strong></p>
<p><code><br />
SELECT c.*, u.firstname AS firstname, u.lastname AS lastname FROM comments AS c<br />
	INNER JOIN<br />
		news AS n<br />
			ON c.news_id = n.id<br />
				INNER JOIN<br />
					users AS u<br />
						ON c.user_id = u.id<br />
							WHERE c.news_id = 1;<br />
</code></p>
<p><strong>7) SELECT TAGS FOR ANY PARTICULAR NEWS (HERE, news_id = 1)</strong>	</p>
<p><code><br />
SELECT t.* FROM tags AS t<br />
	INNER JOIN<br />
		(SELECT tags_news.news_id AS news_id, tags_news.tags_id FROM tags_news WHERE tags_news.news_id = 1) AS tn<br />
			ON t.id = tn.tags_id;<br />
</code>			</p>
<p><strong>8) SELECT NEWS BY TAG NAME (HERE, tag = &#8216;nepal&#8217;)</strong></p>
<p><code><br />
SELECT n.*, t.name AS tag_name, t.slug AS tag_slug FROM news AS n<br />
	INNER JOIN<br />
		tags_news as tn<br />
			ON n.id = tn.news_id<br />
				INNER JOIN tags AS t<br />
					ON tn.tags_id = t.id<br />
						WHERE t.slug = 'nepal';<br />
</code>			</p>
<p><strong>9) SELECT NEWS BY TAG NAME (ALONG WITH COMMENTS COUNT) (HERE, tag = &#8216;nepal&#8217;)	</strong>		</p>
<p><code><br />
SELECT n.*, ifnull(c.count,0) AS comment, t.name AS tag_name, t.slug AS tag_slug, t.id AS tag_id FROM news AS n<br />
	INNER JOIN<br />
		tags_news AS tn<br />
			ON (n.id = tn.news_id)<br />
				INNER JOIN tags AS t<br />
					ON (tn.tags_id = t.id)<br />
						LEFT JOIN<br />
							(SELECT COUNT(comments.id) AS count, comments.news_id FROM comments<br />
								GROUP BY comments.news_id) AS c<br />
									ON n.id = c.news_id<br />
										WHERE t.slug = 'nepal'<br />
											ORDER BY n.created_date DESC;<br />
</code></p>
<p><strong>10) SELECT TAG CLOUD</strong>		</p>
<p><code><br />
SELECT t . * , IFNULL( tq.count, 0 ) AS count<br />
	FROM tags AS t<br />
		LEFT JOIN (<br />
			SELECT COUNT( tags_news.id ) AS count, tags_news.tags_id<br />
				FROM tags_news<br />
					GROUP BY tags_news.tags_id<br />
						) AS tq ON t.id = tq.tags_id<br />
							ORDER BY t.name ASC<br />
								LIMIT 15;<br />
</code><br />
Hope this helps. And thanks for reading.  </p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=779&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-join-filter-select-and-sort-attributes-fields-and-tables/' rel='bookmark' title='Permanent Link: Magento: Join, filter, select and sort attributes, fields and tables'>Magento: Join, filter, select and sort attributes, fields and tables</a></li>
<li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/fun-with-strings-in-php-part-1/' rel='bookmark' title='Permanent Link: Fun with strings in PHP (Part 1)'>Fun with strings in PHP (Part 1)</a></li>
<li><a href='http://blog.chapagain.com.np/sql-error-cannot-insert-the-value-null-into-column-column-does-not-allow-nulls-insert-fails/' rel='bookmark' title='Permanent Link: SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.'>SQL Error: Cannot insert the value NULL into column&#8230; column does not allow nulls. INSERT fails.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/very-useful-sql-queries-with-joins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Magento: Upgrading mysql setup of a module</title>
		<link>http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/</link>
		<comments>http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 08:30:53 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[upgrade]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=363</guid>
		<description><![CDATA[Suppose, you have a module called MyModule. Its version is 0.1.0. Now, you want to do some database changes for the module. You have the mysql setup file (mysql install file) mysql4-install-0.1.0.php in MyModule/sql/mymodule_setup folder of your module. You don&#8217;t need to make direct changes to database. You can upgrade your module to make your [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/' rel='bookmark' title='Permanent Link: Magento: Adding attribute from MySql setup file'>Magento: Adding attribute from MySql setup file</a></li>
<li><a href='http://blog.chapagain.com.np/magento-set-title-keywords-and-description-in-your-module/' rel='bookmark' title='Permanent Link: Magento: Set title, keywords and description in your module'>Magento: Set title, keywords and description in your module</a></li>
<li><a href='http://blog.chapagain.com.np/magento-access-denied-in-admin-of-custom-module/' rel='bookmark' title='Permanent Link: Magento: Access denied in admin of custom module'>Magento: Access denied in admin of custom module</a></li>
<li><a href='http://blog.chapagain.com.np/magento-read-write-xml/' rel='bookmark' title='Permanent Link: Magento: Read Write XML'>Magento: Read Write XML</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Suppose, you have a module called <strong>MyModule</strong>. Its version is <strong>0.1.0</strong>. Now, you want to do some database changes for the module. You have the mysql setup file (mysql install file) <strong>mysql4-install-0.1.0.php</strong> in <strong>MyModule/sql/mymodule_setup</strong> folder of your module.</p>
<p>You don&#8217;t need to make direct changes to database. You can upgrade your module to make your necessary database changes. To do so,</p>
<p><span id="more-363"></span></p>
<p>1) You need to create a new mysql upgrade file inside <strong>MyModule/sql/mymodule_setup</strong> folder.</p>
<p>Let us suppose that you are going to change the version of your module from 0.1.0 to 0.1.1.</p>
<p>The mysql upgrade file name should be <strong>mysql4-upgrade-0.1.0-0.1.1.php</strong></p>
<p>The upgrade format is like <em>mysql4-upgrade-CURRENT_VERSION-UPGRADED_VERSION.php</em></p>
<p>2) Write necessary sql statements in the newly added mysql upgrade file.</p>
<p>3) You have to change the version in <strong>MyModule/etc/config.xml</strong> as well.</p>
<p>Change the version like <strong><version>0.1.1</version></strong>. 0.1.1 is the new version for our module.</p>
<p>4) Reload your site. And you are done!</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=363&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-adding-attribute-from-mysql-setup-file/' rel='bookmark' title='Permanent Link: Magento: Adding attribute from MySql setup file'>Magento: Adding attribute from MySql setup file</a></li>
<li><a href='http://blog.chapagain.com.np/magento-set-title-keywords-and-description-in-your-module/' rel='bookmark' title='Permanent Link: Magento: Set title, keywords and description in your module'>Magento: Set title, keywords and description in your module</a></li>
<li><a href='http://blog.chapagain.com.np/magento-access-denied-in-admin-of-custom-module/' rel='bookmark' title='Permanent Link: Magento: Access denied in admin of custom module'>Magento: Access denied in admin of custom module</a></li>
<li><a href='http://blog.chapagain.com.np/magento-read-write-xml/' rel='bookmark' title='Permanent Link: Magento: Read Write XML'>Magento: Read Write XML</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Magento: Describing Flat Catalog</title>
		<link>http://blog.chapagain.com.np/magento-describing-flat-catalog/</link>
		<comments>http://blog.chapagain.com.np/magento-describing-flat-catalog/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 12:36:06 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[EAV]]></category>
		<category><![CDATA[flat catalog]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=283</guid>
		<description><![CDATA[Difference between EAV and Flat Catalog In EAV database model, data are stored in different smaller tables rather than storing in a single table. Like, product name is stored in catalog_product_entity_varchar table product id is stored in catalog_product_entity_int table product price is stored in catalog_product_entity_decimal table EAV database model is used by Magento for easy [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-reindex-data-programmatically/' rel='bookmark' title='Permanent Link: Magento: Reindex Data Programmatically'>Magento: Reindex Data Programmatically</a></li>
<li><a href='http://blog.chapagain.com.np/magento-create-catalog-price-rule-programmatically/' rel='bookmark' title='Permanent Link: Magento: Create Catalog Price Rule Programmatically'>Magento: Create Catalog Price Rule Programmatically</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/' rel='bookmark' title='Permanent Link: Magento: How to filter product collection using 2 or more category filters?'>Magento: How to filter product collection using 2 or more category filters?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-get-category-name-and-url-from-product/' rel='bookmark' title='Permanent Link: Magento: Get category name and url from product'>Magento: Get category name and url from product</a></li>
<li><a href='http://blog.chapagain.com.np/magento-adding-category-attributes/' rel='bookmark' title='Permanent Link: Magento: Adding category attributes'>Magento: Adding category attributes</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Difference between EAV and Flat Catalog</p>
<p>In EAV database model, data are stored in different smaller tables rather than storing in a single table.</p>
<p>Like,<br />
product name is stored in catalog_product_entity_varchar table<br />
product id is stored in catalog_product_entity_int table<br />
product price is stored in catalog_product_entity_decimal table</p>
<p><span id="more-283"></span></p>
<p>EAV database model is used by Magento for easy upgrade and development as this model gives more flexibility to play with data and attributes.</p>
<p>When flat catalog is enabled in Magento then all the above product attributes (id, name, price) are kept in one table named like catalog_product_flat. Then Magento fetches product data from the flat table rather than joining all the other smaller tables.</p>
<p>There are two types of Flat Catalog:<br />
1) Flat Catalog Product<br />
2) Flat Catalog Category</p>
<p>- Flat Categories are recommended for any Magento installation for improved performance.<br />
- Flat Products is designed and recommended for catalogs that have over 1000 SKU&#8217;s.</p>
<p>Enable Flat Catalog Category:</p>
<p>- Go to Admin Panel->System->Cache Management->Rebuild Flat Catalog Category<br />
- Go to Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Category = Yes</p>
<p>Enable Flat Catalog Product:</p>
<p>- Go to Admin Panel->System->Cache Management->Rebuild Flat Catalog Product<br />
- Go to Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product = Yes</p>
<p>Remember that, initially the selection list of</p>
<p>Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product<br />
OR,<br />
Admin Panel->System->Configuration->Catalog->Frontend->Use Flat Catalog Product</p>
<p>is non-editable. You have to rebuild Flat Catalog from Cache Management. Only then the selection list becomes editable.</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=283&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/magento-reindex-data-programmatically/' rel='bookmark' title='Permanent Link: Magento: Reindex Data Programmatically'>Magento: Reindex Data Programmatically</a></li>
<li><a href='http://blog.chapagain.com.np/magento-create-catalog-price-rule-programmatically/' rel='bookmark' title='Permanent Link: Magento: Create Catalog Price Rule Programmatically'>Magento: Create Catalog Price Rule Programmatically</a></li>
<li><a href='http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/' rel='bookmark' title='Permanent Link: Magento: How to filter product collection using 2 or more category filters?'>Magento: How to filter product collection using 2 or more category filters?</a></li>
<li><a href='http://blog.chapagain.com.np/magento-get-category-name-and-url-from-product/' rel='bookmark' title='Permanent Link: Magento: Get category name and url from product'>Magento: Get category name and url from product</a></li>
<li><a href='http://blog.chapagain.com.np/magento-adding-category-attributes/' rel='bookmark' title='Permanent Link: Magento: Adding category attributes'>Magento: Adding category attributes</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/magento-describing-flat-catalog/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using database in PEAR and Smarty</title>
		<link>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/</link>
		<comments>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 06:00:57 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[PEAR]]></category>
		<category><![CDATA[Smarty]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[mdb2]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=71</guid>
		<description><![CDATA[For using database, you need to install a package of PEAR called ‘MDB2’ along with the installation of PEAR and Smarty. MDB2 provides a common API for all support RDBMS. Connecting to database To connect to a database through PEAR::MDB2, you have to create a valid DSN &#8211; data source name. This DSN consists in [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-pear/' rel='bookmark' title='Permanent Link: An Introduction to PEAR'>An Introduction to PEAR</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For using database, you need to install a package of PEAR called ‘MDB2’ along with the installation of <a title="Installing PEAR" href="http://pear.php.net/manual/en/installation.php" target="_blank">PEAR</a> and <a title="Installing Smarty" href="http://blog.chapagain.com.np/index.php/2008/07/11/installing-the-smarty-template-engine/" target="_blank">Smarty</a>.</p>
<p>MDB2 provides a common API for all support RDBMS.</p>
<p><span id="more-71"></span></p>
<p><strong>Connecting to database</strong></p>
<p>To connect to a database through PEAR::MDB2, you have to create a valid <acronym>DSN &#8211; data source name</acronym>. This DSN consists in the following parts:</p>
<p class="MsoNormal" style="text-align: justify;">phptype: Database backend used in PHP (i.e. mysql , pgsql etc.)</p>
<p class="MsoNormal" style="text-align: justify;">dbsyntax: Database used with regards to SQL syntax etc.</p>
<p class="MsoNormal" style="text-align: justify;">protocol: Communication protocol to use ( i.e. tcp, unix etc.)</p>
<p class="MsoNormal" style="text-align: justify;">hostspec: Host specification (hostname[:port])</p>
<p class="MsoNormal" style="text-align: justify;">database: Database to use on the DBMS server</p>
<p class="MsoNormal" style="text-align: justify;">username: User name for login</p>
<p class="MsoNormal" style="text-align: justify;">password: Password for login</p>
<p class="MsoNormal" style="text-align: justify;">proto_opts: Maybe used with protocol</p>
<p class="MsoNormal" style="text-align: justify;">option: Additional connection options in URI query string format. options get separated by &amp;.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The DSN can either be provided as an associative array or as a string. The string format of the supplied DSN is in its fullest form:</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value</em></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">To instantiate a database object you have several methods available using MDB2.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>factory()</em> : Will instantiate a new <span>MDB2_Driver_Common</span> instance, but will not connect to the database until required. This will delay making the actual connection.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>connect()</em> : Will instantiate a new <span>MDB2_Driver_Common</span> instance, and will establish a database connection immediately. This way any connection issues will immediately raise an error.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>singleton()</em> : Returns a MDB2_Driver_Common instance.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">A new MDB2_Driver_Common object is only created once using <em>factory()</em>, subsequent calls to singleton will return a reference to the existing object. This method is preferred over declaring your database object as a global.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">To connect to a database you have to use the function <em>factory()</em>, <em>connect()</em> or <em>singleton()</em>, which require a valid DSN as the first parameter. This parameter can either be a string or an array. The second parameter is the optional $options array that can contain runtime configuration settings for this package.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">I have used the <em>connect()</em> function with the DSN as</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>phptype://username:password@hostspec/database</em></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php; title: ; notranslate">$mdb2 =&amp; MDB2::connect('mysql://root:@localhost/test');</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">In my case, the database backend is mysql, database username is root, database password is null, the hostname is localhost and the database name is test.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong>Querying the database</strong></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">PEAR MDB2 provides several methods for querying databases. The most direct method is query(). It takes a SQL query string as an argument. There are two possible returns: A new MDB2_Result object for queries that return results (such as SELECT queries), or a MDB2_Error object on failure. It should not be used with statements that manipulate data (such as INSERT queries).</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">exec() should be used for manipulation queries. There are two possible returns: An integer denoting the number of affected rows for statements that manipulate data (such as INSERT queries), or a MDB2_Error object on failure. It should not be used with statements that return results (such as SELECT queries).</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">Here, I will only be dealing with the SELECT queries.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php; title: ; notranslate">$mdb2 = new ConnectMDB2;&lt;/p&gt;
// calling the function conMDB2 of ConnectMDB2 class
$connect = $mdb2-&gt;conMDB2();&lt;/p&gt;
$res = $connect-&gt;query(&quot;SELECT * FROM basic&quot;);
</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong>Fetching result</strong></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The MDB2_Result_Common object provides four methods for fetching data from rows of a result set: fetchOne(), fetchRow(), fetchCol() and fetchAll().</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">fetchRow() and fetchOne() read an entire row or a single field from a column respectively. The result pointer gets moved to the next row each time these methods are called. NULL is returned when the end of the result set is reached.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">fetchAll() and fetchCol() read all rows in the result set and therefore move the result pointer to the end. While fetchAll() reads the entire row data, fetchCol() only reads a single column.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">MDB2_Error is returned if an error is encountered.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">I have used the fetchRow() method. The fetchmode MDB2_FETCHMODE_ASSOC is used for fetching data. The default fetchmode is MDB2_FETCHMODE_ORDERED.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php; title: ; notranslate">while($row[] = $res-&gt;fetchRow(MDB2_FETCHMODE_ASSOC))
{
$result = $row;
}
</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">If there is any error then the error message is displayed in a separate template called error.tpl. And if there is no error while fetching data then the fetched result is displayed in the template file index.tpl.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php; title: ; notranslate">if(PEAR::isError($res))
{
// get error message
$error =&amp; $res-&gt;getMessage();&lt;/p&gt;
// assign error message
$smarty-&gt;assign('errmessage',$error);&lt;/p&gt;
// display the message and exit
$smarty-&gt;display('error.tpl');
exit();
}&lt;/p&gt;
// asign the content
$smarty-&gt;assign('result',$result);

// display the content
$smarty-&gt;display('index.tpl');
</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">In the template file, <em>foreach</em> loop is used to display the result.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: xml; title: ; notranslate">{foreach from=$result item=view}
&lt;tr bgcolor=&quot;{cycle values=&quot;#ffffff,#eeeeee&quot;}&quot;&gt;
&lt;td&gt;{$view.firstname}&lt;/td&gt;
&lt;td&gt;{$view.lastname}&lt;/td&gt;
&lt;/tr&gt;
{/foreach}
</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong><span><a title="Download Source Code" href="http://chapagain.googlecode.com/files/database_pear_smarty.zip">Download source code</a></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a title="Download Smarty" href="http://smarty.php.net/download.php" target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip">http://chapagain.googlecode.com/files/smarty.zip</a></p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=71&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-pear/' rel='bookmark' title='Permanent Link: An Introduction to PEAR'>An Introduction to PEAR</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Backup and Recovery of MySQL database</title>
		<link>http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/</link>
		<comments>http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 12:08:18 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[recovery]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/2007/10/10/backup-and-recovery-of-mysql-database/</guid>
		<description><![CDATA[1) To make a backup of a single MySQL database You have to make backup before you enter inside your MySQL system i.e. by typing mysql –u username –p password . (Here, username and password indicates your MySQL server username and password.) Hence, go to your MySQL root path. Type “mysqldump –u username –p password [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/joomlapack-simply-the-best-backup-component-for-joomla/' rel='bookmark' title='Permanent Link: JoomlaPack &#8211; Simply the best backup component for Joomla!'>JoomlaPack &#8211; Simply the best backup component for Joomla!</a></li>
<li><a href='http://blog.chapagain.com.np/google-chrome-backup-profile-bookmarks-history-extensionsbest-and-easy-way/' rel='bookmark' title='Permanent Link: Backup Google Chrome Profile Bookmarks History Extensions: Best and Easy way'>Backup Google Chrome Profile Bookmarks History Extensions: Best and Easy way</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="text-align: justify">1) To make a backup of a single MySQL database<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">You have to make backup before you enter inside your MySQL system i.e. by typing <em>mysql –u username –p password</em> . (Here, username and password indicates your MySQL server username and password.)<o:p> </o:p></p>
<p><span id="more-23"></span></p>
<p class="MsoNormal" style="text-align: justify">Hence, go to your MySQL root path.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Type “mysqldump –u username –p password database_name > backup.sql” (without quote)<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Here, <em>database_name</em> is the name of the database of which you want to create backup and <em>backup.sql</em> is the backup file name. You can name it as yourname.sql or whatever you like.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">The file <em>backup.sql</em> created from the above procedure will be saved inside the ‘bin’ directory (root directory) of the MySQL server.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">You can specify other location as well for the file <em>backup.sql</em> .</p>
<p class="MsoNormal" style="text-align: justify">Type “mysqldump –u username –p password database_name > E:/backup.sql” (without quote)<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">This will save the file to the drive E [for windows OS]<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Hope, unix, linux users know it well to specify location path. <span style="font-family: Wingdings"><span></span></span><o:p><br />
</o:p></p>
<p class="MsoNormal" style="text-align: justify">Incase, your MySQL server password is blank, you need not have to type <em>–p password</em>.</p>
<p class="MsoNormal" style="text-align: justify">You can type</p>
<p class="MsoNormal" style="text-align: justify">mysqldump –u username database_name > backup.sql<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Or, if your MySQL username and password both are blank than you can type</p>
<p class="MsoNormal" style="text-align: justify">mysqldump database_name > backup.sql<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">&nbsp;</p>
<p class="MsoNormal" style="text-align: justify">2) To make a backup of multiple MySQL database<span>  </span></p>
<p class="MsoNormal" style="text-align: justify">Type</p>
<p class="MsoNormal" style="text-align: justify">mysqldump –u username –p password &#8211;databases db1 db2 db3 > threedb.sql<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Here, db1, db2, db3 are three different database name. You can use more databases, similarly.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">&nbsp;</p>
<p class="MsoNormal" style="text-align: justify">3) To make a backup of all MySQL databases<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Type</p>
<p class="MsoNormal" style="text-align: justify">mysqldump –u username –p password &#8211;all-databases > alldb.sql<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">&nbsp;</p>
<p class="MsoNormal" style="text-align: justify">4) To read back the backup we have made i.e. to create tables and databases from the backup file<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">If you had made a backup file of a single database, then you should create a database in your MySQL server. Then type “mysql –u username –p password db_name < backup.sql”<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Here, <em>db_name</em> is the database name in your MySQL server. All tables are created inside this database from the <em>backup.sql</em> file.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">If you had made a backup file of multiple databases, then you don’t have to create database beforehand. The backup file consists of the query to create all the databases and tables. Note it here, that you don’t have to specify the database name. You just have to type “mysql –u username –p password < backup.sql” and that’s all.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Note: Be careful to specify the proper location of your backup.sql file.<o:p> </o:p></p>
<p class="MsoNormal" style="text-align: justify">Enjoy!!!</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=23&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/joomlapack-simply-the-best-backup-component-for-joomla/' rel='bookmark' title='Permanent Link: JoomlaPack &#8211; Simply the best backup component for Joomla!'>JoomlaPack &#8211; Simply the best backup component for Joomla!</a></li>
<li><a href='http://blog.chapagain.com.np/google-chrome-backup-profile-bookmarks-history-extensionsbest-and-easy-way/' rel='bookmark' title='Permanent Link: Backup Google Chrome Profile Bookmarks History Extensions: Best and Easy way'>Backup Google Chrome Profile Bookmarks History Extensions: Best and Easy way</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
<li><a href='http://blog.chapagain.com.np/mysql-database-foreign-key-understanding-and-implementation/' rel='bookmark' title='Permanent Link: MySQL Database: Foreign Key Understanding and Implementation'>MySQL Database: Foreign Key Understanding and Implementation</a></li>
<li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Server doesn’t start</title>
		<link>http://blog.chapagain.com.np/mysql-server-doesnt-start-2/</link>
		<comments>http://blog.chapagain.com.np/mysql-server-doesnt-start-2/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 23:24:41 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[start]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=17</guid>
		<description><![CDATA[Problem: “Fails to start the service. Please wait 30 seconds to try again.” I faced this problem 2 years back when I was a newbie to MySQL. I was just starting to learn PHP and MySQL at that time. [PS: The problem was faced and tackled in Windows platform.] Solution: Well, there is my.ini file [...]


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/' rel='bookmark' title='Permanent Link: Magento: Upgrading mysql setup of a module'>Magento: Upgrading mysql setup of a module</a></li>
<li><a href='http://blog.chapagain.com.np/wamp-xampp-localhost-server-not-working/' rel='bookmark' title='Permanent Link: WAMP XAMPP: Localhost server not working'>WAMP XAMPP: Localhost server not working</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong></p>
<p class="MsoNormal" style="text-align:justify;"><span class="msgbodytext">“<em>Fails to start the service. Please wait 30 seconds to try again.</em>” </span></p>
<p class="MsoNormal" style="text-align:justify;"><span class="msgbodytext">I faced this problem 2 years back when I was a newbie to MySQL. I was just starting to learn PHP and MySQL at that time. </span></p>
<p><span id="more-54"></span></p>
<p class="MsoNormal" style="text-align:justify;">[PS:<strong> </strong>The problem was faced and tackled in Windows platform.]<span class="msgbodytext"> </span></p>
<p class="MsoNormal" style="text-align:justify;"><span class="msgbodytext"><strong>Solution:</strong><br />
</span></p>
<p class="MsoNormal" style="text-align:justify;"><span class="msgbodytext">Well, there is my.ini file to control the server. It might be in your </span></p>
<p>C:/Documents and Settings/Administrator/WINDOWS/my.ini</p>
<p class="MsoNormal" style="text-align:justify;">[I suppose that your Operating System is installed in C: drive]</p>
<p class="MsoNormal" style="text-align:justify;">Here, “Administrator” is the user with the administrator privilege. This could be different as you might have the admin user with your name.</p>
<p class="MsoNormal" style="text-align:justify;">Copy the my.ini file from the above location to</p>
<p>C:/WINDOWS/my.ini</p>
<p class="MsoNormal" style="text-align:justify;">And you are done! Your mysql server will start quite happily.</p>
<p class="MsoNormal" style="text-align:justify;">Cheers,</p>
<p class="MsoNormal" style="text-align:justify;">Mukesh</p>
<hr /><small>Copyright &copy; 2011<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=54&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/mysql-installation-problem/' rel='bookmark' title='Permanent Link: MySQL Installation Problem'>MySQL Installation Problem</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
<li><a href='http://blog.chapagain.com.np/alter-mysql-table-to-add-drop-column-add-foreign-key/' rel='bookmark' title='Permanent Link: Alter MySQL table to add &#038; drop column &#038; add Foreign Key'>Alter MySQL table to add &#038; drop column &#038; add Foreign Key</a></li>
<li><a href='http://blog.chapagain.com.np/magento-upgrading-mysql-setup-of-a-module/' rel='bookmark' title='Permanent Link: Magento: Upgrading mysql setup of a module'>Magento: Upgrading mysql setup of a module</a></li>
<li><a href='http://blog.chapagain.com.np/wamp-xampp-localhost-server-not-working/' rel='bookmark' title='Permanent Link: WAMP XAMPP: Localhost server not working'>WAMP XAMPP: Localhost server not working</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/mysql-server-doesnt-start-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
