MAGENTO中MYSQL数据操作

如果是在外部引用magento的sql语句,首先要先加载magento中的核心文件mage.php

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

1.对magento中的数据进行select:Set the connection for Read the query :

getConnection('core_read'); ?>

$connectionRead object to execute the fetch query:

select()
->from('tablename', array('*'))
->where('id=?',1)
->group('name');
$row =$connectionRead->fetchRow($select);   //return rows

$result = $connectionRead->fetchAll($select); // This query will return a multidimensional Array.This work as a  mysql_fetch_array() ?>

2.对magento中数据进行插入:
Set the connection for Write query :

getConnection('core_write');  ?>

$connectionWrite object will be used to execute insert query.

beginTransaction();
$data = array();
$data['firstname']= 'abc';
$data['lastname']='cba';
$data['email']='[email protected]';
$connectionWrite->insert('tablename', $data);
$connectionWrite->commit();   ?>

3.对magento中数据进行更新:
$connectionWrite object will be used to execute update query.

beginTransaction();
$data = array();
$data['firstname'] = 'xyz';
$where = $connectionWrite->quoteInto('tableId =?', '5');
$connectionWrite->update('tablename', $data, $where);
$connectionWrite->commit();  ?>

4.对magento中数据进行删除:
$connectionWrite object will be used to execute delete query.

beginTransaction();
$condition = array($connectionWrite->quoteInto('tableId=?', '5'));
$connectionWrite->delete('tablename', $condition); ?>

注:在对个sql语句中多个条件时可以使用数组:比如查询语句中多个where条件:

    $write = Mage::getSingleton("core/resource")->getConnection('core_write');
    $table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
    $write->beginTransaction();
    $where = $write->quoteInto(array('quote_id =?'=>$quoteId,'address_type =?'=>'shipping'));
    $write->update($table,array('shipping_method'=>'tang_mycarrier'),$where);
    $write->commit();

你可能感兴趣的:(MAGENTO中MYSQL数据操作)