Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

Can we query a different database on the same server (localhost)?

Can we query a different database on the same server (localhost)?

I have Metamod (as well as Chameleon) installed on my site but wanted to create a module that loads only when certain conditions are met from a separate sql database that resides on the same server. The database and user are different but they are hosted on localhost as well. Is this possible with either Metamod or Chameleon? I suppose so, considering php code can be used. If so, can someone point me in the right direction to run a query on a separate database?

Thanks!
-Moni

thegrindlab
Beginner Modder
ranks
useravatar
Offline
7 Posts
User info in posts
Administrator has disabled public posting

Re: Can we query a different database on the same server (localhost)?

Hi Moni,

you should be able to do arbitrary database lookups using standard php mysql calls, e.g.

Code:

$db_host = 'localhost';

$db_user = 'the_db_username';
$db_pwd = 'the_db_password';

$database = 'database_name';
$table = 'the_table_to_query';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    return; // Can't connect to database

if (!mysql_select_db($database))
    return; // Can't select database

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    return; // Query to show fields from table failed
}

$fields_num = mysql_num_fields($result);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    // assuming that row has columns 'id' and 'name':
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);

Instead of the printf you might want to check the row that's returned to see if it equals some condition, then return a particular module id based on that.

Cheers,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1679
Total Polls:
6
Total Posts:
5913
Dormant:
User Info:
 
Total Users:
10746
Newest User:
victor1234
Members Online:
0
Guests Online:
226

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked