Wednesday, August 22, 2012

Backing Up The Database via SSH/Telnet

In order to back up your database via SSH or Telnet you will require 2 things:

1) SSH or Telnet access to your site. You will need to check with your hosting company to see if this is available.

2) An SSH/Telnet Client, such as PuTTy.

Open your SSH/Telnet client and log into your website. The command line prompt you will see will vary by OS.
For most hosting companies, this will bring you into the FTP root folder.

Type in the following to create a backup in the current directory:

mysqldump --opt -Q -u dbusername -p databasename > backupname.sql

Or to create a backup in a separate directory (signified by /path/to/) type:

mysqldump --opt -Q -u dbusername -p databasename > /path/to/backupname.sql

You will be prompted for the database password. Enter it and the database will backup.

If your hosting company has you on a remote MySQL server, such as mysql.yourhost.com, you will need to add the servername to the command line. The servername will be the same as in your config.php. The command line will be:

Current directory:

mysqldump --opt -Q -h servername -u dbusername -p databasename > backupname.sql

Separate directory:

mysqldump --opt -Q -h servername -u dbusername -p databasename > /path/to/backupname.sql

Thursday, August 16, 2012

PHP: Get all tables from database


<?php
$dbname = 'mysql_dbname';

if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    echo "Table: {$row[0]}\n";
}

mysql_free_result($result);
?>

Magento: Get admin user’s data (id, name, username, email, password, etc).


$userArray = Mage::getSingleton('admin/session')->getData();

$user = Mage::getSingleton('admin/session');
$userId = $user->getUser()->getUserId();
$userEmail = $user->getUser()->getEmail();
$userFirstname = $user->getUser()->getFirstname();
$userLastname = $user->getUser()->getLastname();
$userUsername = $user->getUser()->getUsername();
$userPassword = $user->getUser()->getPassword();

Magento: Update layout or add js with controller



public function myAction()
{

    /* ... Some code ...*/

    $update = $this->getLayout()->getUpdate();

    /* ... Some code ...*/

    $this->addActionLayoutHandles();

    /* ... Some code ...*/

    $this->loadLayoutUpdates();

    /* ... Some code ...*/

    $update->addUpdate('
        <reference name="head">
            <action method="addJs"><script>varien/product.js</script></action>
            <action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params /><!--<if/><condition>can_load_calendar_js</condition>--></action>
            <action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if /><condition>can_load_calendar_js</condition>--></action>
            <action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if /><condition>can_load_calendar_js</condition>--></action>
            <action method="addItem"><type>skin_js</type><name>js/bundle.js</name></action>
        </reference>
    ');

    /* ... Some code ...*/

    $this->generateLayoutXml()->generateLayoutBlocks();

    /* ... Some code ...*/

    $this->renderLayout();

}


Wednesday, August 15, 2012

Magento: Admin page not found error

After moving magento to live server or demo server. If not able to access admin page or got page not found error. apply below sql query.

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;