简体   繁体   中英

MySQL query with/without table specific reference

In Joomla 2.5.14, when I create a query to MySQL using PHP, like:

$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:

$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. 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.

What is the issue with the prefix? I don't understand how it could cause you an problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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