Forums

[resolved] accessing external database (5 posts)

  1. clairence
    Member
    Posted 10 months ago #

    Oddly, I used the search function and it returned zero results, although I'm sure there are many questions about this.

    In any case... I want to be able to call an external database from inside a page and/or post. How do I do that?

    I tried the following in a page (using 'allow php in posts and pages' plugin), and get errors indicating "no database selected":

    [php]
    
    function main_db_login() {
    $passwrd='secretpw';
    $user='mysitemain';
    $db='mysite_main';
    $dbcnx = mysql_connect('localhost',$user,$passwrd);
    if (!$dbcnx) {
    echo '<p>Unable to connect to the WordPress DB at this time.</p>';
    exit();
    }
    }
    
    function contact_db_login(){
    $passwrd='secretpw';
    $user='mysitecontact';
    $db='mysite_contact';
    $dbcnx = mysql_connect('localhost',$user,$passwrd);
    if (!$dbcnx) {
    echo '<p>Unable to connect to the contacts DB at this time.</p>';
    exit();
    }
    }
    
     function users_db_login(){
    $passwrd='secretpw';
    $user='mysiteusers';
    $db='mysite_users';
    $dbcnx = mysql_connect('localhost',$user,$passwrd);
    if (!$dbcnx) {
    echo '<p>Unable to connect to the users DB at this time.</p>';
    exit();
    }
    }
    [/php]
    
    [php]
    
    mysql_close();
    contact_db_login();
    $dabble=mysql_query("select * from messages");
    while($row=mysql_fetch_array($dabble)){
    	print_r($row);
    }
    mysql_close();
    echo'closed';
    
    users_db_login();
    $dibble=mysql_query("select * from basic");
    while($row=mysql_fetch_array($dibble)){
    	print_r($row);
    }
    mysql_close();
    echo'closed';
    main_db_login();
    
    [/php]

    Any help is appreciated.

  2. apljdi
    Member
    Posted 10 months ago #

    So you are trying to connect to multiple databases that are hosted on the same physical machine on which this script runs?

  3. clairence
    Member
    Posted 10 months ago #

    yes.

  4. fonglh
    Member
    Posted 10 months ago #

    exactly what the error message says. you haven't selected a database.
    http://php.net/manual/en/function.mysql-select-db.php

  5. clairence
    Member
    Posted 10 months ago #

    So many times, I looked at the error message, and looked at the script... and just didn't see how I wasn't selecting a database. But I see you're right; I was logging in but not to a specific database.

    How foolish of me.

    Thanks for the nudge.

Reply

You must log in to post.

About this Topic