美文网首页
MAGENTO中MYSQL数据操作

MAGENTO中MYSQL数据操作

作者: 帅马 | 来源:发表于2015-11-16 16:31 被阅读444次

如果是在外部引用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();

相关文章

  • MAGENTO中MYSQL数据操作

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

  • MySQL 基础操作

    · MySQL中不分大小写· MySQL中分为DML(数据操作语言) 和 DDL(数据定义语音) DML(数据操作...

  • 8.mysql之异步操作

    mysql之异步操作 普通的mysql操作是同步操作,插入数据的速度(即I/O读写)远远低于spider中解析数据...

  • 3 MySQL数据库操作

    2 MySQL数据库操作 3.1 MySQL操作数据库 3.2 MySQL操作数据表 3.2.1 MySQL创建数...

  • 基于Linux的MySQL操作实例(软件安装,mysql基本操作

    基于Linux的MySQL操作实例(软件安装,mysql基本操作,mysql数据类型,建表、插入数据操作) 前言 ...

  • Django事务操作

    在Django中实现数据库的事务操作 在学习MySQL数据库时,MySQL数据库是支持原子操作的. 什么是数据库的...

  • python对mysql的操作

    python对mysql的操作 Mysql 常见操作 数据库操作 PS:用户权限相关数据保存在mysql数据库的u...

  • 005——MySQL

    基础 配置phpmyadmin MySQL重启 PHP中操作MySQL的基本代码和流程 测试 循环MySQL 数据...

  • shell中的sql操作

    在编写shell脚本的时候,可能会遇到操作mysql数据库的情况。下面介绍如何在shell脚本中操作mysql数据...

  • MySql笔记

    Mac安装并运行MySql MySql数据库、数据表的操作 MySql数据类型及常见约束 MySql表操作 MyS...

网友评论

      本文标题:MAGENTO中MYSQL数据操作

      本文链接:https://www.haomeiwen.com/subject/bobkhttx.html