简体   繁体   English

MySQL查询有/无表特定参考

[英]MySQL query with/without table specific reference

In Joomla 2.5.14, when I create a query to MySQL using PHP, like: 在Joomla 2.5.14中,当我使用PHP创建对MySQL的查询时,例如:

$query = "SELECT id FROM xmb9d_content WHERE state=1" ;

It all works fine, but if I don't want a specific reference to the database prefix (xmb9d_) and use: 一切正常,但是如果我不想特定引用数据库前缀(xmb9d_)并使用:

$query = "SELECT id FROM #__content WHERE state=1" ;

The query isn't executed. 查询未执行。 Is this the right way of building the query or what is wrong with this code? 这是构建查询的正确方法还是此代码有什么问题?

You need to use the database prefix and also stick to Joomla 2.5 coding standards. 您需要使用数据库前缀,并且还要遵守Joomla 2.5编码标准。 There shouldn't be any problems with the prefix, providing your query is correct. 只要您的查询正确,前缀就不会有任何问题。

This is how it should look: 它应该是这样的:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('id')
 ->from('#__content')
 ->where('state = 1');

$db->setQuery($query);

$results = $db->loadObjectList();

xmb9d_content is the name of the table by replacing it with #__content you are attempting to run the query on a table that doesn't exist (i assume) so it wont work. xmb9d_content是表的名称,通过将其替换为#__content来尝试在不存在(我假设)的表上运行查询,因此它将无法正常工作。

What is the issue with the prefix? 前缀有什么问题? I don't understand how it could cause you an problems 我不明白这怎么可能给您带来麻烦

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

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