-1

我在 Magento DB 中添加了一个额外的表,称为 kv_mailings。一些额外的产品信息存储在那里,与 Magento 没有任何关系。这就是为什么我添加了一个额外的表。

我使用了这个答案中的 test.php 想法,我的文件如下所示:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM kv_mailings WHERE sku=hkag";
$row = $connection->fetchRow($sql); //fetchRow($sql), fetchOne($sql),...

Zend_Debug::dump($row);

我的桌子目前看起来像这样:

当前表

这就是疯狂的事情开始的地方。当我的查询是SELECT * FROM kv_mailings WHERE sku=SKU它将选择正确的行并返回:

array(9) {
  ["id"] => string(1) "1"
  ["titel"] => string(19) "value"
  ["sku"] => string(3) "SKU"
  ["logo"] => string(96) "value"
  ["background"] => string(84) "value"
  ["codes_new"] => string(0) ""
  ["codes_old"] => string(419) "value"
  ["codes_enc"] => string(6) "CODE11"
  ["terms"] => string(708) "value"
}

*请注意,我已将实际值更改为value.

如果我将查询更改为,SELECT * FROM kv_mailings WHERE sku=hkag我将收到以下错误消息:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'hkag' in 'where clause'' in /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Statement/Pdo.php:228 Stack trace: #0 /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array) #1 /var/www/webshop.test.vtnl.net/https/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) #2 /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array) #3 /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array) #4 /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT * FROM k...', Array) #5 /var/www/webshop.test.vtnl.net/https/lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('SELECT * FROM k...', Array) #6 /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Adapt in /var/www/webshop.test.vtnl.net/https/lib/Zend/Db/Statement/Pdo.php on line 234

那是怎么回事?如您所见,表中有一条记录,其中 sku = hkag... 有谁知道发生了什么以及我能做些什么?非常感谢!

4

3 回答 3

7

在您的 Select 语句hkag中是一个字符串,所以它应该在引号中

 $sql = "SELECT * FROM kv_mailings WHERE sku='hkag'";

为了安全起见,这样做会更好(请参阅MySQL 准备好的语句

$coreResource = Mage::getSingleton('core/resource');
$connection = $coreResource->getConnection('core_read');

$select = $connection->select()
    ->from( $coreResource->getTableName('{moduleName}/kv_mailings'))), //->from('kv_mailings')
    ->where('sku = ?', 'hkag');

$row = $connection->fetchOne($select);

或者

  $sql = "SELECT * FROM kv_mailings WHERE sku=?";
  $row = $connection->fetchRow($sql, 'hkag' );

看看@http ://www.vortexcommerce.com/blog/magento-direct-sql

于 2013-07-04T15:29:28.230 回答
0

您的 sql 语句是内容字符串,因此您必须写入字符串才能签入您的 magento 数据库

$sql = "SELECT * FROM kv_mailings WHERE sku='hkag'";

你可以试试下面的代码

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM kv_mailings WHERE sku='hkag'";
$row = $connection->fetchRow($sql); //fetchRow($sql), fetchOne($sql),...

Zend_Debug::dump($row);

让我知道我是否可以为您提供更多帮助。

于 2013-07-04T15:38:26.133 回答
0
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "select * from test"
$row = $connection->fetchRow($sql); //fetchRow($sql), fetchOne($sql),...

you can check which method is available in this $connection  object by simple way:

echo "<pre>" ;
print_r(get_class_methods($connections));


please check below url for more reference 

Magento Magento中的自定义sql查询 :如何选择、插入、更新和删除数据?

于 2013-11-01T18:50:12.383 回答