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

    if i want to make a mysql query in a snippet, do i need to open a new mysql connection or does it use the one from modx directly?

    if yes - is there a function or include, sothat its a little cleaner?
      • 26435
      • 1,193 Posts
      If you use the MODx DBAPI and execute a query it will check if a connection to the db is established, and if not, open one.
      Use
      <?php
      $modx->db->query("SELECT * FROM yourtablename");
      ?>


      -sD-
      Dr. Scotty Delicious, Scientist.
        Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
        All of the above... in no specific order.


        I send pointless little messages
        • 8895
        • 76 Posts
        thank you mr. delicious! grin
          • 8895
          • 76 Posts
          ok, seems i got a different problem undecided
          i have the following code:
          $simplevideoshow = null;
          
          if (class_exists('simplevideoshow')) {
             $simplevideoshow = new simplevideoshow($_video, $_parameter);
          } else {
          	$output=$_lang['error'].__LINE__.") ".$filevalue." ".$_lang['invalid_class'];
          	return $output;
          }
          
          // ---------------------------------------------------------------------------------------------------------
          $med_id = 1;
          // Query's
          	// query - name, type and comment:
          	$result = $modx->db->query("SELECT media_name, comment FROM media WHERE media_id = '".$med_id."'");
          	if (!$result) {die('QueryErr: VideoShow.inc';}
          
          	$med_name = mysql_result($result, 0);
          	$med_comment = mysql_result($result, 1);
          
          // Output
          $output=$simplevideoshow->render();
          
          $medhd="</font><table width='100%' cellspacing='0' cellpadding='0' border='1'>
          	<tr><td align='center'>";
          
          $medft="</td></tr>
          	<tr><td width='100%'>
          		<table border='0' cellspacing='0' cellpadding='2' width='100%'>
          		<tr><td>
          			Comment: 
          		</td><td>
          			$med_comment
          		</td></tr>
          		<tr><td>
          			Rating: 
          		</td><td>
          			$med_name
          		</td></tr>
          
          
          	</table><br /><br />";
          
          $output=$medhd.$output.$medft;
          // ---------------------------------------------------------------------------------------------------------
          
            // ---------------------------------------------------
            // Output results
            // ---------------------------------------------------
          
          return $output;


          which i implemented into an existing snippet. but all i get is a white page undecided
          when i comment out the sql query it shows me an error message (that there is no result to get/as there is no query)

          what could be the problem?
            • 8895
            • 76 Posts
            ok i found something rather suspicious shocked
            as mentioned, i get a white page.

            at the beginning of the page (when i rightclik - view source on the modx page) i found this:
            <?php die('Unauthorized access.'); ?>a:37:{s:2:"id";s:1:"7";s:4:"type";s:8:"document";s:11:"contentType";s:9:"text/html";s:9:"pagetitle";s:5:"Clips";s:9:"longtitle";s:13:"Chaotic Clips";s:11:"description";s:0:"";s:5:"alias";s:0:"";s:9:"published";s:1:"1";s:8:"pub_date";s:1:"0";s:10:"unpub_date";s:1:"0";s:6:"parent";s:1:"0";s:8:"isfolder";s:1:"1";s:9:"introtext";s:0:"";s:7:"content";s:180:"<font size="5"> <span style="font-weight: bold;">The Clips!</span></font>
            <br /><br /><font color='white'>
            [!SimpleVideoShow? &video=`test.wmv` &enableJavaScript=`true`!]";s:8:"richtext";s:1:"1";s:8:"template";s:1:"6";s:9:"menuindex";s:1:"3";s:10:"searchable";s:1:"1";s:9:"cacheable";s:1:"1";s:9:"createdby";s:1:"1";s:9:"createdon";s:10:"1164102166";s:8:"editedby";s:1:"1";s:8:"editedon";s:10:"1164900153";s:7:"deleted";s:1:"0";s:9:"deletedon";s:1:"0";s:9:"deletedby";s:1:"0";s:11:"publishedon";s:10:"1164118592";s:11:"publishedby";s:1:"1";s:9:"menutitle";s:5:"Clips";s:7:"donthit";s:1:"0";s:11:"haskeywords";s:1:"0";s:11:"hasmetatags";s:1:"0";s:10:"privateweb";s:1:"0";s:10:"privatemgr";s:1:"0";s:13:"content_dispo";s:1:"0";s:8:"hidemenu";s:1:"0";s:17:"__MODxDocGroups__";s:0:"";}<!--__MODxCacheSpliter__--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
            "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
            <head> (....)


            i got a bad feeling embarrassed
            can anyone tell me why?
            i think its definitely got something to do with the mysql query.
            my query looks like this:
            $result = $modx->db->query("SELECT med.........


            i checked the config file and all the database settings are correct - i even tried login in with mysql query browser to doublecheck and it worked. -> HELP!
              • 25663 MODX Staff
              • 12,272 Posts
              The source on the MODx page is the cached code, and will look like that. In the front end when surfing your site, what does the source look like?
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                • 8895
                • 76 Posts
                you mean the actual one i entered?
                <font size="5"> <span style="font-weight: bold;">The Clips!</span></font>
                <br /><br /><font color='white'>
                [!SimpleVideoShow? &video=`test.wmv` &enableJavaScript=`true`!]


                and the code of the snippet is:
                $snipPath = $modx->config['base_path'] . 'assets/snippets/simplevideoshow/';
                $snip = $snipPath.'simplevideoshow.inc.php';
                
                if (isset($file)) {
                $file = $file;
                } else {
                $file = $snip;
                }
                
                $include = include($file);
                
                return $include;


                and the code for the include is:
                global $modx;
                
                  // ---------------------------------------------------
                  // Get Language
                  // ---------------------------------------------------
                
                $language = (isset($language))? $language : "english";
                
                  // ---------------------------------------------------
                  // Include files
                  // ---------------------------------------------------
                
                $files['language'] = $snipPath."lang/$language.inc.php";
                $files['functions'] = $snipPath."simplevideoshow.class.php";
                
                foreach ($files as $filename => $filevalue) {
                	if (file_exists($filevalue) && $filename != "language") {
                		include_once($filevalue);
                	} else if(file_exists($filevalue) &&  $filename == "language") {
                		include($filevalue);
                		$GLOBALS['_lang'] = $_lang;
                	} else if($filename == "language") {
                		$output="Language file does not exist Please check: ".$filevalue;
                		return $output;
                	} else {
                		$output=$_lang['error'].__LINE__.") ".$filevalue." ".$_lang['file_does_not_exist'];
                		return $output;
                	}
                }
                
                  // ---------------------------------------------------
                  // Parameters
                  // ---------------------------------------------------
                
                $video = isset($video) ? $video: $_lang['error'].__LINE__.") ".$_lang['no_video'];
                $videoPath =  isset($videoPath) ? $videoPath : "assets/media/";
                if (!file_exists( $modx->config['base_path'] . $videoPath.$video)){
                		$output=$_lang['error'].__LINE__.") ". $modx->config['base_path'] . $videoPath.$video." ".$_lang['file_does_not_exist']."<br />";
                		return $output;	
                }
                $_video['video']=$modx->config['site_url'].$videoPath.$video;
                $_video['width'] = isset($width) ? $width: "400";
                $_video['height'] = isset($height) ? $height: "340";
                $_video['standby'] = isset($standby) ? $standby: $_lang['standby'];
                
                $_parameter['showcontrols'] = isset($showcontrols) ? $showcontrols: "true";
                $_parameter['autostart'] = isset($autostart) ? $autostart: "true";
                $_parameter['controller'] = isset($controller) ? $controller: "true";
                $_parameter['showstatusbar'] = isset($showstatusbar) ? $showstatusbar: "default";
                $_parameter['volume'] = isset($volume) ? $volume: "75%";
                $_parameter['animationatstart'] = isset($animationatstart) ? $animationatstart: "default";
                $_parameter['transparentatstart'] = isset($transparentatstart) ? $transparentatstart: "true"; //modifié
                $_parameter['qtsrcdontusebrowser'] = isset($qtsrcdontusebrowser) ? $qtsrcdontusebrowser: "default";
                $_parameter['enablejavascript'] = isset($enablejavascript) ? $enablejavascript: "default";
                $_parameter['clicktoplay'] = isset($enablejavascript) ? $enablejavascript: "default";
                $_parameter['stretchToFit'] = isset($stretchToFit) ? $stretchToFit: "true";
                $_parameter['AutoSize'] = isset($stretchToFit) ? $stretchToFit: "true";
                
                
                  // ---------------------------------------------------
                  // Begin the traitement
                  // --------------------------------------------------
                
                $simplevideoshow = null;
                
                if (class_exists('simplevideoshow')) {
                   $simplevideoshow = new simplevideoshow($_video, $_parameter);
                } else {
                	$output=$_lang['error'].__LINE__.") ".$filevalue." ".$_lang['invalid_class'];
                	return $output;
                }
                
                // ---------------------------------------------------------------------------------------------------------
                $med_id = 1;
                // Query's
                	// query - name, type and comment:
                	$result = $modx->db->query("SELECT media_name, comment FROM media WHERE media_id = '".$med_id."'");
                	if (!$result) {die('QueryErr: VideoShow.inc';}
                
                	while ($val = mysql_fetch_array($result, MYSQL_NUM))
                	{
                		$med_name = $val[0];
                		$med_comment = $val[1];
                	}
                
                // Output
                $output=$simplevideoshow->render();
                
                $medhd="</font><table width='100%' cellspacing='0' cellpadding='0' border='1'>
                	<tr><td align='center'>";
                
                $medft="</td></tr>
                	<tr><td width='100%'>
                		<table border='0' cellspacing='0' cellpadding='2' width='100%'>
                		<tr><td>
                			Comment: 
                		</td><td>
                			$med_comment
                		</td></tr>
                		<tr><td>
                			Rating: 
                		</td><td>
                			$med_name
                		</td></tr>
                
                
                	</table><br /><br />";
                
                $output=$medhd.$output.$medft;
                // ---------------------------------------------------------------------------------------------------------
                
                  // ---------------------------------------------------
                  // Output results
                  // ---------------------------------------------------
                
                return $output;

                  • 8895
                  • 76 Posts
                  help?
                    • 10746
                    • 126 Posts
                    $include = include($file);
                    
                    return $include;


                    What is the actual effect of these lines?

                    The include "function" (it is not really a function) has some restrictions on when you can actually treat a "return" statement from the included file as the returned value...

                    To check this, I would put in an extra line of code to make sure that the variable $include is actually getting assigned the correct value...

                    Otherwise, just use the included file to ASSIGN the value to "$output" and then return it

                    include($file);   // this no longer has a return, but simply creates a variable $output
                    return $output;
                    



                    Hope this helps..

                    Gordon
                      • 10487 MODX Staff
                      • 1,535 Posts
                      Just on a first glance, you may want to check this line as well:
                      if (!$result) {die('QueryErr: VideoShow.inc';}

                      It’s missing a closing parenthesis for the die() statement, it should be:
                      if (!$result) {die('QueryErr: VideoShow.inc');}

                      Also, for me, I’ve sometimes had blank pages with including files, so check that all your file paths being passed into the include statements are being parsed correctly. You could also try using include_once() instead of include() - I’ve had some issues around that before as well.

                      Hope that helps smiley
                        Garry Nutting
                        Senior Developer
                        MODX, LLC

                        Email: [email protected]
                        Twitter: @garryn
                        Web: modx.com