Showing posts with label custom data base connection in magento. Show all posts
Showing posts with label custom data base connection in magento. Show all posts

delete all orders and customers magento

I am magento developer and i know when we worked on test site than there is lot of dummy order and customer created on magento website . We used our local DB to live site db when we will go for live magento website. but before we can't delete dummy order or customer record in data base table. now i wrote a very easy script to delete all orders and customers in magento.

One More Important Thing for we don't need to open phpmyadmin for delete all orders and customers in magento

Note :- Please take a Data Base backup before run this script  .

Step1:-  Create a deleteCustomerAndOrder.php file in root folder

Step2:-  paste below code and save the file.


<?php

require_once ("app/Mage.php");
$app = Mage::app('default');

$config  = Mage::getConfig()->getResourceConnectionConfig("default_setup");

$dbinfo = array("host" => $config->host,
            "user" => $config->username,
            "pass" => $config->password,
            "dbname" => $config->dbname
);

$hostname = $dbinfo["host"];
$user = $dbinfo["user"];
$password = $dbinfo["pass"];
$dbname = $dbinfo["dbname"];
$con = mysql_select_db($dbname,mysql_connect($hostname,$user,$password)) ;
$tablePrefix = (string) Mage::getConfig()->getTablePrefix();


// For Delete orders tables

$orderTableName = array (
    'sales_flat_order',
    'sales_flat_order_address',
    'sales_flat_order_grid',
    'sales_flat_order_item',
    'sales_flat_order_status_history',
    'sales_flat_quote',
    'sales_flat_quote_address',
    'sales_flat_quote_address_item',
    'sales_flat_quote_item',
    'sales_flat_quote_item_option',
    'sales_flat_order_payment',
    'sales_flat_quote_payment',
    'sales_flat_shipment',
    'sales_flat_shipment_item',
    'sales_flat_shipment_grid',
    'sales_flat_invoice',
    'sales_flat_invoice_grid',
    'sales_flat_invoice_item',
    'sendfriend_log',
    'tag',
    'tag_relation',
    'tag_summary',
    'wishlist',
    'log_quote',
    'report_event'
    );
mysql_query('SET FOREIGN_KEY_CHECKS=0');
for($ga=0;$ga<=(count($orderTableName)-1);$ga++){

  mysql_query('TRUNCATE  `'.$tablePrefix.$orderTableName[$ga].'`');
  mysql_query('ALTER TABLE  `'.$tablePrefix.$orderTableName[$ga].'` AUTO_INCREMENT=1');


}
// For Delete customer tables

$customerTableName = array (
    'customer_address_entity',
    'customer_address_entity_datetime',
    'customer_address_entity_decimal',
    'customer_address_entity_int',
    'customer_address_entity_text',
    'customer_address_entity_text',
    'customer_address_entity_varchar',
    'customer_entity',
    'customer_entity_datetime',
    'customer_entity_decimal',
    'customer_entity_int',
    'customer_entity_text',
    'customer_entity_varchar',
    'log_customer',
    'log_visitor',
    'log_visitor_info',
    'log_visitor_info',
'eav_entity_store');


for($gau=0;$gau<=(count($customerTableName)-1);$gau++){

  mysql_query('TRUNCATE  `'.$tablePrefix.$customerTableName[$gau].'`');
  mysql_query('ALTER TABLE  `'.$tablePrefix.$customerTableName[$gau].'` AUTO_INCREMENT=1 ');

}

mysql_query('SET FOREIGN_KEY_CHECKS=1');

?>


Step3:- run this script on your browser.
http://yoursiteurl.com/deleteCustomerAndOrder.php

Guys I am always try to learn new things and  post on my blog for our magento developers . Please give me your valuable feedback for encourage me to write more programming solutions.
Thanks
GA





get table prefix in magento

when we will work on Data Base table with custom Query . than it is very important to know what is the table prefix of the magento data base table. This magento blog or magento tutorial will tell you how to get the table Prefix in magento.

In 2 ways you will get data base table prefix.

1) echo $tablePrefix = (string) Mage::getConfig()->getTablePrefix();

2) Go to app/etc/local.xml open local.xml file. and see

                <db>
                <table_prefix><![CDATA['SEE YOUR TABLE PREFIX HERE ']]></table_prefix>
               </db>


magento how to select insert update and delete data

It is every important post because if we work on our custom module or extension than each time we need to communicate with our data base table.
If you want custom change on your Data base table in easy way than this post is helpful for you.

//For Select Query
$db_read = Mage::getSingleton('core/resource')->getConnection('core_read');
$tablePrefix = (string) Mage::getConfig()->getTablePrefix();

$sql = 'SELECT COLUMN_NAME FROM `' . $tablePrefix . 'TABLE_NAME`';

$data = $db_read->fetchAllRow($sql); // fetch All row in a table
$data = $db_read->fetchRow($sql); // fetch single row in a table
 print_r($data);  // to see the output of table


// For Insert Query.
 $db_write1 = Mage::getSingleton('core/resource')->getConnection('core_write');
        $tablePrefix = (string) Mage::getConfig()->getTablePrefix();

        $sql = 'INSERT INTO `' . $tablePrefix . 'TABLE_NAME`
VALUES ('COLUMN1_VALUE', 'COLUMN1_VALUE ')';
        $db_write1->query($sql);  


// For Delete Query
 $sql2 = 'DELETE FROM `' . $tablePrefix . 'TABLE_NAME` WHERE TABLE_COLUMN=COLUMN_VALUE';
 $db_write1->query($sql2);        
             

// For Update Query
 $updateQue = 'UPDATE `' . $tablePrefix . 'TABLE_NAME` SET TABLE_COLUMN1=COLUMN_VALUE1, TABLE_COLUMN2=COLUMN_VALUE2 WHERE TABLE_COLUMN=COLUMN_VALUE';
 $db_write1->query($updateQue);


Do you want custom data base connection in magento see here