Mysql data query and the sharp-pound underbar thin

2019-09-12 16:18发布

I have a mysql query that looks like this $query="SELECT * FROM #__content". I have no idea what the #__ before content is supposed to mean ? If I have created an article X of category Y, then how can I refer to the mysql table that contains X in Joomla ?

3条回答
甜甜的少女心
2楼-- · 2019-09-12 16:28

#__ is simply the database table prefix and is defined in you configuration.php

If it wasn't defined, people would have to manually have to input their prefixes into every extension that requires access to the database, which you can imagine would be annoying.

So for example, if you database table prefix is j25, then:

#__content = j25_content

查看更多
霸刀☆藐视天下
3楼-- · 2019-09-12 16:39

As others have said, hash underscore sequence '#_' is the prefix used for table names by Joomla!'s JDatabase class. (N.B. there is only one underscore, the second underscore is maintained to for readability in table names.)

When you first setup Joomla! you are given the option of setting a prefix or using the one randomly generated at the time. You can read about how to check the prefix here.

When you access the database using the JDatabase class it provides you with an abstraction mechanism so that you can interact with the database that Joomla is using without you having to code specifically for MySQL or MSSQL or PostgreSQL etc.

When JDatabase prepares a query prior to executing it, it replaces any occurrences #_ in the from segment of the query with the prefix setup when Joomla! was installed. e.g.

    // Get the global DB object.        
    $db = JFactory::getDBO();
    // Create a new query object.       
    $query = $db->getQuery(true);
    // Select some fields
    $query->select('*');

    // Set the from From segment
    $query->from('#__myComponents_Table');

Later when you execute the query JDatabase will change the from segment of the SQL from

from #__myComponents_Table to

from jp25_myComponents_Table — if the prefix is jp25, prior to executing it.

查看更多
地球回转人心会变
4楼-- · 2019-09-12 16:43

#__ is a prefix of your tables

查看更多
登录 后发表回答