We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27449
    • 211 Posts
    I want do connect to database.
    I create a snipped with this code:

    <?php
    function connection()
    {
    if (!($link=mysql_connect("localhost","root","")))
    {
    echo "Error to connection data base.";
    exit();
    }
    if (!mysql_select_db("testeDB",$link))
    {
    return "Error to select data base.";
    exit();
    }
    return $link;
    }

    $link=connection();
    return "Established Connection.
    ";

    mysql_close($link); //close connection
    ?>

    and the after I call the snipped on the document, but doesn’t work.

    How I connect the database?

      • 4310
      • 2,310 Posts
      MODx automatically connects to the MODx DB.
      If you want to run a query then just use something like this in a snippet :
      = $modx->db->query('SELECT * FROM `modx_site_content` WHERE `id`=1')

      Have a look at the DB API wiki pages.
        • 27449
        • 211 Posts
        but I want create another database for to save my information...
        I to be able save my information on the modx database or I have to create another database?
          • 16183
          • 1,390 Posts
          Quote from: sjscosta at Sep 29, 2008, 04:11 AM

          but I want create another database for to save my information...
          I to be able save my information on the modx database or I have to create another database?

          If you want to create another database you can do that and then have mox connect to your database. You can have two situations

          1. Different Database, Same Server
          2. Different Database, Different Server

          Read the wiki here, on this link: http://wiki.modxcms.com/index.php/Access_another_database_from_MODx on how to go about connecting.

          If on the other hand you want to use modx database, you can do that, even create your own tables. Am no expert on this and if am wrong someone else will give you the correct position. At least am sure of the above wink

          cheers/k
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Life is so much easier if you can use the same database; unless it’s a really busy site, it should not cause any performance problems. Then you can just use the same database connection, with different table prefixes than what you used for the MODx tables (modx_ by default).
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org