简体   繁体   English

连接到Joomla 2.5数据库并获得类别ID的最简单方法

[英]Easiest way to connect to Joomla 2.5 database and get a Category ID

My Situation 我的情况

Im very new to Joomla and I installed a component called EasyBlog. 我对Joomla不太熟悉,我安装了一个名为EasyBlog的组件。 What Im trying to do is get category_id from jos_easyblog_post table for a particular post_id and and echo it on the template. 我想做的是从jos_easyblog_post表中获取一个特定的post_id category_id ,并在模板上echo它。 Im also not quite sure if its ok to put database connection script on the template itself? 林也不太确定是否可以将数据库连接脚本放在模板本身上?

jos_easyblog_post has few columns, jos_easyblog_post列很少,

id - Post Id
category_id - Category that post belongs to
Hits etc etc.

Lets say id which is Post ID is 5 and how can I conenct to the database and go to jos_easyblog_post and look for the post id 5 and get the category_id associated with it? 假设id为Post ID为5,我如何连接到数据库并转到jos_easyblog_post并查找post id 5并获取与之关联的category_id Thanks guys. 多谢你们。

The joys of using a CMS such as Joomla, is that scripts become easy. 使用CMS(如Joomla)的乐趣在于脚本变得容易。 Connecting to the database is done using the code below: 使用以下代码连接到数据库:

$db = JFactory::getDbo();

To get results from a database table using Joomla 2.5 standards, you can try something like this: 要使用Joomla 2.5标准从数据库表中获取结果,您可以尝试执行以下操作:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('category_id')
 ->from('#__easyblog_post')
 ->where('post_id = 5');
$db->setQuery($query);
$row = $db->loadResult();

echo $row;

Note that when defining a Joomla database table, the prefix is defined as #__ 请注意,在定义Joomla数据库表时,前缀定义为#__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM