简体   繁体   中英

joomla 2.5 virtuemart sql fetch db

I'm trying to create a module for joomla 2.5. I try to get some elements from db and after that I want to echo em.

In phpmyadmin I tried this

SELECT * FROM `uhhu_virtuemart_manufacturers` WHERE virtuemart_manufacturer_id=7 

I take back 1 record, so its working. now I try this with php:

<?php
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('uhhu_virtuemart_manufacturers'); 
$query->where('virtuemart_manufacturer_id = 7');     
$db->setQuery($query);
$options = $db->loadObjectList();
return $options;
if ($db->getErrorNum()) {
  echo $db->getErrorMsg();
  exit;
}

Nothing is happening, tried it with many similar ways, but I failed. Anyone have a idea?

Firstly, you should escape all values and column name using quote() and quoteName() . Secondly, you should use the generic database table prefix ( #__ ) which means if you ever change the prefix name, you won't need to update your query. Thirdly, you are not actually trying to output any results. Have a look at the following:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName(array('*')))
      ->from($db->quoteName('#__virtuemart_manufacturers')) 
      ->where($db->quoteName('virtuemart_manufacturer_id')  . ' = 7');     
$db->setQuery($query);

$options = $db->loadObjectList();

foreach($options as $row) {
    echo $row->NAME_OF_COLUMN;
}

Update:

To connect to another database, you can use the following and then run your query

$option = array(); //prevent problems

$option['driver']   = 'mysql';            // Database driver name
$option['host']     = 'db.myhost.com';    // Database host name
$option['user']     = 'fredbloggs';       // User for database authentication
$option['password'] = 's9(39s£h[%dkFd';   // Password for database authentication
$option['database'] = 'bigdatabase';      // Database name
$option['prefix']   = 'abc_';             // Database prefix (may be empty)

$db = JDatabaseDriver::getInstance( $option );

$query = $db->getQuery(true);   
$query->select($db->quoteName(array('*')))
      ->from($db->quoteName('#__virtuemart_manufacturers')) 
      ->where($db->quoteName('virtuemart_manufacturer_id')  . ' = 7');     
$db->setQuery($query);   
$options = $db->loadObjectList();

foreach($options as $row) {
    echo $row->NAME_OF_COLUMN;
}

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