We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8807
    • 9 Posts
    I’m building a snippet to select and display some data from a database that is on the same host as modx, but a seperate db and tables.

    I’ve got the snippet like:

    <?php
    $conn2 = mysql_connect("localhost","myusername","mypassword") or die ("Cant connect to Database");
    $db2 = mysql_select_db("mydatabase") or die ("database gone.");

    $sql1 = "select field1.table1, field2.table2
    from table1, table2
    where field1.table1 = field2.table2";
    $result = mysql_query($sql1) or die ("SQL1 not working, check snippet");

    mysql_data_seek($result, 0);
    $i = 0;
    while($result_ar = mysql_fetch_assoc($result)){
    ?>etc etc etc....

    On accessing the document Modx displays "« MODx Parse Error »
    MODx encountered the following error while attempting to parse the requested resource:
    « Execution of a query to the database failed - Table ’mydatabase.modx_site_templates’ doesn’t exist »
    SQL: SELECT templatename,id FROM `modx_site_templates` "

    Any help appreciated!
      • 22303 MODX Staff
      • 10,725 Posts
      What you are doing is called hijacking the current database connection. You need to create and maintain a separate connection handle for your connection to the separate database. See the fourth parameter for mysql_connect for more information on creating a new connection and then you’ll need to use that connection handle in your calls to other mysql_ functions (e.g. see the second parameters for mysql_select_db and mysql_query).
        • 8807
        • 9 Posts
        Quote from: OpenGeek at Sep 05, 2007, 09:32 PM

        What you are doing is called hijacking the current database connection. You need to create and maintain a separate connection handle for your connection to the separate database. See the fourth parameter for mysql_connect for more information on creating a new connection and then you’ll need to use that connection handle in your calls to other mysql_ functions (e.g. see the second parameters for mysql_select_db and mysql_query).

        Thanks, your right. Changing the line to read:

        $conn2 = mysql_connect("localhost","myusername","mypassword", newlink) or die ("Cant connect to Database");

        Got it to work.