We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20646
    • 61 Posts
    Hi everybody,

    sorry for my poor english.
    I have created a snipped call [!connection!], this snippet is for connection to a another database mysql.
    $host="thehost";
    $name="thename";
    $mdp="pwd";
    $base="mabase";
    $bd = mysql_connect("$host", "$name", "$mdp") or die("Mauvais identifiants bdd !");
    mysql_select_db($base, $bd) or die("Impossible de se connecter à la table !");
    

    I would like to include this snippet in another snippet, like a include of connection in PHP.
    How to do that?
    ex:
    include [!connection!]
    $sql = "select * from news";
    

    thanks a lot
      • 33337
      • 3,975 Posts
      To run the snippet inside another snippet, you need to use runSnippet: http://wiki.modxcms.com/index.php/API:runSnippet
      For future reference: http://svn.modxcms.com/docs/display/Evo1/runSnippet
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 20646
        • 61 Posts
        ok thanks for your reply.
        I set
        $html = $modx->runSnippet('connection');

        but after in my snippet who call the first , I test to see if the $base can be display.
        But this variable is not been transmit between the two snippet.
        I must use global var?
        thank you
          • 33337
          • 3,975 Posts
          Re-reading your post, if u want to access another database, please see: http://wiki.modxcms.com/index.php/Access_another_database_from_MODx
            Zaigham R - MODX Professional | Skype | Email | Twitter

            Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • 20646
            • 61 Posts
            also you say I must don’t use runSnippet but DBAPI?

            Quote from: zi at Jan 21, 2010, 10:30 AM

            Re-reading your post, if u want to access another database, please see: http://wiki.modxcms.com/index.php/Access_another_database_from_MODx
              • 3296
              • 16 Posts
              I’m a not programmer so please bear with me.
              So you created a template that querys a database.
              The data that the query is capturing is posted to a page using a snippit
              then that snippit is displayed on a page published on the website.

              That database isn’t a part of the database for the modx.

              The following is just a example so I can see if I’m following your line of thought

              So If I wanted to call the database’

              ---------------------------------------------------
              not a complete statement just a few lines from the php page contactmedirectly.php

              <?php session_start() ?>
              <?
              include "db_registrations.php";
              // defaults
              $name = "";
              $email = "";
              $city = "";
              $comments= "";
              $regtime= "";
              if(isset($_POST[’submit’])){
              $name = substr(trim(strip_tags($_POST[’name’])), 0, 100);
              $email = substr(trim(strip_tags($_POST[’email’])), 0, 100);
              $city = substr(trim(strip_tags($_POST[’city’])), 0, 100);
              $comments = substr(trim(strip_tags($_POST[’comments’])), 0, 1000);
              $regtime = time();
              $tdisplay = date("F j Y h:i", $regtime);
              if($_SESSION["captcha"]==$_POST["captcha"])
              {
              //CAPTHCA is valid; proceed the message: save to database, send by e-mail ...

              $query = "insert into contacts (name, email, city, comments, regtime) values (’$name’, ’$email’, ’$city’, ’$comments’, ’$regtime’)";
              $result = mysql_query($query) or die("Error: Query failed.

              $query

              " . mysql_error());

              html encluded below this line to complete the form to fillout


              -----------------------------------------
              not a complete statement just a few lines from the next php page db_registrations.php

              <?php
              //$mysql_host_reg = "100.100.100.10";
              $mysql_host_reg = "100.100.100.10";
              $mysql_user_reg = "username";
              $mysql_pass_reg = "password";
              $mysql_db_reg = "nameofdatabase";

              $link_reg = mysql_connect($mysql_host_reg, $mysql_user_reg, $mysql_pass_reg) or die("Error: Could not connect to $mysql_host_reg.

              " . mysql_error());
              mysql_select_db($mysql_db_reg) or die("Error: Could not select database $mysql_db_reg.");
              ?>