如何在外部操纵Magento的数据库,首先我们必须取得外部运行的handle
/* external run Magento */
require_once('app/Mage.php');
umask(0);
Mage::app('default'); //blank
//Mage::run(); //current theme layout
接下来是数据库链接的handle
/* get database handle and execute sql*/
$db=Mage::getSingleton('core/resource')->getConnection('core_write');
在magento数据库建立一个自己的表吧,我们在外部执行write的操作创建数据库
/* install sql and insert data*/
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->run("
-- DROP TABLE IF EXISTS news;
CREATE TABLE news (
`news_id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`filename` varchar(255) NOT NULL default '',
`content` text NOT NULL default '',
`status` smallint(6) NOT NULL default '0',
`created_time` datetime NULL,
`update_time` datetime NULL,
PRIMARY KEY (`news_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO news(title,filename,content,status,created_time,update_time) VALUES ('今日头条','文章一','今天广州风光无限','1',now(),now());
");
$installer->endSetup();
$result=$db->query('select * from news');
/*print result*/
if(!$result) {
return FALSE;
}
$rows = $result->fetch(PDO::FETCH_ASSOC);
if(!$rows) {
return FALSE;
}
//print_r($rows); //print by array
foreach($rows as $row=>$value):
print "<pre>$row:$value</pre>";
endforeach;
![](http://hi.csdn.net/attachment/201109/15/0_1316059834gny1.gif)
demo就这么简单了,如果你想加上一些样式或者在特定的位置中显示数据,那么就在对应的文件中写入类似的代码吧!