We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    Hey folks,

    I need a little help with how modx works with databases... I have a couple of examples of code that I would like to have converted to work with modx. I’m learning PHP and I would like to understand how I can use my code in modx. I just need some pointers with the examples I have typed as this will help me better with what I need to do as the wiki and docs haven’t really helped.


    In this example I would like to know how to connect to an external database, create a table and populate it.
    <?php
    $connect = mysql_connect("localhost", "myDB", "username", "password")
    or die("Connection not established");
    
    //creates database
    $create = mysql_query("CREATE DATABASE IF NOT EXISTS storage")
    or die(mysql_error());
    
    //recently created DB is active
    mysql_select_db("storage");
    
    //create table
    $table1 = "CREATE TABLE table1 (
    id int(10) NOT NULL auto_increment,
    file varchar(250) NOT NULL,
    )";
    
    $results = mysql_query($storage)
    or die (mysql_error());
    
    echo "Database created";
    ?>



    With this example, how do I connect to the external DB, go to table1 and display the id and file text?
    <?php
    $connect = mysql_connect("localhost", "myDB", "username", "password")
    or die("Connection not established");
    
    mysql_select_db("storage");
    
    $query = "SELECT id, file " .
    "FROM table1 ".
    "ORDER BY id";
    
    $results = mysql_query($query)
    or die(mysql_error());
    
    while ($row = mysql_fetch_array($results)) {
    extract($row);
    echo $id;
    echo "<br />";
    echo $file;
    }
    ?>
    
    Thanks for any help.
    
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 23491 ☆ A M B ☆
      • 1,056 Posts
      Hey rosco,

      I would recommend you check out this Wiki post: http://wiki.modxcms.com/index.php/Access_another_database_from_MODx

      My advice is to leverage the existing DB API whenever possible smiley
        Mike Reid - www.pixelchutes.com
        MODx Ambassador / Contributor
        [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
        ________________________________
        Where every pixel matters.
        • 3749
        • 24,544 Posts
        You beat me to it. I was just about to post an identical message. wink
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 25551 ☆ A M B ☆
          • 1,231 Posts
          I tried this code

          <?php
          $host = "localhost";
          $database = "storage";
          $uid = "root";
          $pwd = "mypass";
          $db = new DBAPI($host,$database, $uid,$pwd)
          or die ("no connection");
          $ds = $db->select('*','table1')
          or die("table not found");
          echo $ds;
          ?>


          and my result is...

          Resource id #16

          My database has a table called table1 with id and file fields. I don’t see where Resource id #16 comes from.
            Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
            AugmentBLU - MODX Partner

            BLUcart - MODX Revolution E-Commerce & Shopping Cart
            • 23491 ☆ A M B ☆
            • 1,056 Posts
            Quote from: rossco at Mar 11, 2009, 05:53 PM

            and my result is...

            Resource id #16

            My database has a table called table1 with id and file fields.  I don’t see where Resource id #16 comes from. 

            This is the PHP pointer to your MySQL SELECT result set.

            Try the following in place of "echo $ds":

            while( $row = $modx->fetchRow( $ds ) ) {
            
            print_r($row);
            
            }
            
              Mike Reid - www.pixelchutes.com
              MODx Ambassador / Contributor
              [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
              ________________________________
              Where every pixel matters.
              • 25551 ☆ A M B ☆
              • 1,231 Posts
              That worked. Thanks!


              Array
              (
              [id] => 4
              [file] => test/file
              )

                Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                AugmentBLU - MODX Partner

                BLUcart - MODX Revolution E-Commerce & Shopping Cart