服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

magento中MYSQL数据操作分析

时间:2016-01-10 04:34来源:未知 作者:最模板 点击:
如果是在外部引用magento的sql语句,首先要先加载magento中的核心文件mage.php require_once ../../app/Mage.php;Mage::app(default); 1.对magento中的数据进行select:Set the connection for Read the query : ?php $c

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

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

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

<?php $connectionRead = Mage::getSingleton('core/resource')->getConnection('core_read'); ?>

$connectionRead object to execute the fetch query:

<?php   $select = $connectionRead->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 :

<?php  $connectionWrite = Mage::getSingleton('core/resource')->getConnection('core_write');  ?>

$connectionWrite object will be used to execute insert query.

<?php   $connectionWrite->beginTransaction();
$data = array();
$data['firstname']= 'abc';
$data['lastname']='cba';
$data['email']='abc@gmail.com';
$connectionWrite->insert('tablename', $data);
$connectionWrite->commit();   ?>

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

<?php   $connectionWrite->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.

<?php   $connectionWrite->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();
 

(责任编辑:最模板)
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容