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

    I am new to MODx and starting my exploration of this wonderful solution.
    I ran in the following problem.
    I would like to query another database from my 1st custom snippet but could not find the right way to do it...
    It always crashes my modx install : cannot use MODx anymore after using the snippet.

    I am starting with this
    $mydb= new DBAPI();
    $mydb->connect($host=’localhost’,$dbase=’mybase’, $uid=’myuser’,$pwd=’mypass’);
    then I have my former sql statements
    $result = mysql_query("select count(*)...

    and ended with
    $mydb->disconnect();

    I can save my snippet and test it. After test everything is broken.
    I get error messages like the following one :
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ... Passed Id is NaN !

    Thanks for your help,



    • The DBAPI abstracts mysql_query() calls -- you should be using one of the DBAPI methods, i.e. $mydb->query(), $mydb->select(), $mydb->getValue(), etc.
        • 12332
        • 8 Posts
        After a lot of tries it finally works for my custom snippet : I can use another database !
        Yet I dont feel comfortable with the DBAPI : any error corrupts my MODX instance and I need to reinstall and reload my setup from scratch.

        Is this situation normal?
        Is there a way to revert back once MODX corrupted instead of reinstalling ? huh

        • Quote from: Honoré at May 04, 2006, 07:24 PM

          After a lot of tries it finally works for my custom snippet : I can use another database !
          Yet I dont feel comfortable with the DBAPI : any error corrupts my MODX instance and I need to reinstall and reload my setup from scratch.

          Is this situation normal?
          Is there a way to revert back once MODX corrupted instead of reinstalling ? huh

          I’m sorry, I don’t understand the problem. What is being corrupted and by what? If anything is being corrupted I would imagine it would be from issuing SQL statements that affect the data. If you could explain in a little more detail how something is getting corrupted by connecting to a DB through the DBAPI, perhaps I could assist you a little more to figure this out...
            • 12332
            • 8 Posts
            Oups... I think I found where my problem is.

            The snippet was calling the following function :

            function GetCartId()
            	{
            		// This function will generate an encrypted string and
              	// will set it as a cookie using set_cookie. This will
            		// also be used as the cookieId field in the cart table
            		if(isset($_COOKIE["id"]))
            		{
            			return $_COOKIE["id"];
            		}
            		else
            		{
            			// There is no cookie set. We will set the cookie
            			// and return the value of the users session ID			
            			error_reporting(0);
                                    session_start();
            			setcookie("id", session_id(), time() + ((3600 * 24) * 30));
            			return session_id();
            		}
            	}


            After calling this piece of code, everything gets corrupted and I am no more able to access the manager or user interfaces. I get a lot of SQL errors. In manager, I cannot retrieve documents or open snippets, chunks, templates...
            Might come from the session_start() ?

            My snippet code is as follows :
            if (empty($_GET["action"]))
              {}
            else
            {
            $action = $_GET["action"];
            $item_id = $_GET["item_id"]; 
            switch($action)
            	{
            	case "add_item":
                         { 
            		    $mydb= new DBAPI();
                    $mydb->connect('localhost','db', 'user','pwd');
                $cookie = GetCartId();
            		$table = 'cart';
            		$where = 'cookie="'.$cookie.'"';
            		$rs = $mydb->select("*", $table, $where, '', '');
            		if ($mydb->getRecordCount($rs) > 10)
            		    {$msg ="The shopping cart is full. You cannot add more than 10 items";}
            	  else
            	       {...
                           $msg = "The item has ben added to your cart";
                           ...}
                    $mydb->disconnect();
                    
                    $modx->setPlaceholder('msg', $msg);  
                         } 
                    }
            }


            • Yeah, you are killing the session with that code. Makes everything break. You can’t do that within a CMS snippet, as it takes over the built-in user/session handling.

              Wait, I spoke too soon, maybe, another potential problem is your call to disconnect() - you do not need to close the connection manually; I believe this may be affecting the ability for MODx to continue making requests to the database after you run the snippet.

              Actually, I may just be an idiot -- it could even be the cookie (well, i thought I could help) tongue
              • Garry Nutting Reply #7, 18 years ago
                What I’ve done recently, whether it’s correct or not, is to create a nested array item in the $_SESSION variable to manage my own session information.

                That way, if I need to access my session info I just use: $_SESSION[’mysession’][’myparam’];

                And to delete my session info, I just use: unset($_SESSION[’mysession’]);

                I’m not sure how that would work when using cookies though ...
                  Garry Nutting
                  Senior Developer
                  MODX, LLC

                  Email: [email protected]
                  Twitter: @garryn
                  Web: modx.com
                  • 12332
                  • 8 Posts
                  Actually it works with the call to disconnect() as it is not disconnecting the main connection to MODx DB ( I am used to doing it for db performance )
                  Hopefully now I dont have the problem anymore. It was starting to get me crazy : I created more than 15 test instances and all crashed !

                  I just need to rewrite this function and test the cookie functionality. I will let you know when I figure out what exactly causes the corruption.
                    • 12332
                    • 8 Posts
                    OK I got it !!

                    it could even be the cookie (well, i thought I could help)
                    You are right => it comes from the setcookie("id",...)
                    "id" seems to be used for user authentification in modx.
                    the function was updating it and then everything was messed up.

                    Thank you for your help !

                    • Please file a bug report referencing this thread... "id" as a reserved word is an obscure thing and a silly oversight on our part.
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me