我在Magento DB中添加了一个名为kv_mailings的额外表格。一些额外的productinfo存储在那里,与Magento没有任何关系。这就是我添加额外表格的原因。
我使用了this answer中的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 ...有谁知道发生了什么以及我能做些什么呢?非常感谢!
答案 0 :(得分:7)
在您的Select语句hkag
中是一个字符串,因此它应该在引用
$sql = "SELECT * FROM kv_mailings WHERE sku='hkag'";
为了安全起见,最好这样做(见MySQL prepared statement)
$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' );
答案 1 :(得分: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);
请告诉我是否可以为您提供更多帮助。
答案 2 :(得分: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
Custom sql Query in magento Magento: How to select, insert, update, and delete data?