[英]Joomla 3.4 Component with Postgres db connection
i'm developing a joomla component to read some data from a postgres database. 我正在开发一个joomla组件,以从postgres数据库读取一些数据。 Joomla is installed on mysql.
Joomla安装在mysql上。 In the model i have
在模型中我有
protected function getListQuery()
{
$option = array(); //prevent problems
$option['driver'] = 'postgresql'; // Database driver name
$option['host'] = '192.168.1.0'; // Database host name:port number
$option['port'] = '1111';
$option['user'] = 'user'; // User for database authentication
$option['password'] = 'password'; // Password for database authentication
$option['database'] = 'PGDB'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$dbstock = JDatabaseDriver::getInstance( $option );
$query = $dbstock->getQuery(true);
$query->select('*')->from($dbstock->quoteName('myTable'));
return $query;
}
I get errors from the view becouse it seems that the dabase server doesn't like quotes in the table name: 我从视图中收到错误,因为dabase服务器似乎不喜欢表名中的引号:
You have an error in your SQL syntax;
您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near "myTable" LIMIT 20' at line 2 SQL=SELECT * FROM "myTable" LIMIT 20
检查与您的MySQL服务器版本相对应的手册,以在第2行的“ myTable” LIMIT 20'附近使用正确的语法SQL = SELECT * FROM“ myTable” LIMIT 20
What can I do? 我能做什么?
why the hell this works?? 为什么这行得通呢?
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
class SLCatalogModelProducts extends JModelList
{
public function __construct($config = array())
{
parent::__construct($config);
$option = array(); //prevent problems
$option['driver'] = 'postgresql'; // Database driver name
$option['host'] = '192.168.1.1'; // Database host name:port number
$option['user'] = 'user'; // User for database authentication
$option['password'] = 'password'; // Password for database authentication
$option['database'] = 'PGSQL'; // Database name
$option['prefix'] = '';
$db = JDatabaseDriver::getInstance( $option );
parent::setDbo($db);
}
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
$option = array(); //prevent problems
$option['driver'] = 'postgresql'; // Database driver name
$option['host'] = '192.168.1.1'; // Database host name:port number
$option['user'] = 'user'; // User for database authentication
$option['password'] = 'password'; // Password for database authentication
$option['database'] = 'PGSQL'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$dbstock = JDatabaseDriver::getInstance($option);
$query = $dbstock->getQuery(true);
$query->select('*')
->from('myTable');
return $query;
}
}
Really don't like this code...but it works :-( 确实不喜欢此代码...但是它可以工作:-(
Change 更改
$query->select('*')->from($dbstock->quoteName('myTable'));
to 至
$query->select('*')->from('myTable');
This should prevent forcing quotes on the table name. 这样可以防止在表名上使用引号。
As stated in the docs , 如文档中所述,
quoteName
QUOTENAME
Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection risks and reserved word conflicts.将SQL语句标识符名称(例如列,表或数据库名称)用引号引起来,以防止注入风险和保留字冲突。
Since your table is not a reserved keyword and you are not processing user input (so eliminating sql injection), you can safely remove that. 由于您的表不是保留关键字,并且您不处理用户输入(因此消除了sql注入),因此可以安全地删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.