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
    I am creating my own snippets and plugins in modx to automate many functions on a website and wondered if there was a more elegant way to update and query the database.

    I have it all working as I want it but wondered if I was doubling up on queries etc. A partial example is below and wondered if this is the right way to do things to fetch database values. Should I name each as $result1 $result2 etc... or is calling them all $result ok? Can I make the 2 results below in to one? I’m using the top $result just to get the appId value to put in the $result1 WHERE appID= is that necessary using a separate database query? I’m not sure if you can get values at the end of a script and place them at the start of the script?

    
    $result = $modx->db->select("appId", "modx_apps" ) or die ("table not found");
    if(mysql_num_rows($result) ) {
    while($row = mysql_fetch_array($result)) {
    		extract($row);
    		$row['appId'];
    		$appId = $row['appId'];
    		}
    	} 
    //get appId and put the value in the result1 WHERE go get the correct tables... 
    
    $result1 = $modx->db->select("appId,tsDone", "modx_apps" , "appId=$appId") or die ("table not found");
    if(mysql_num_rows($result1) > 0) {
    while($row = mysql_fetch_array($result1)) {
    		extract($row);
    		$row['tsDone'];
    		$tsDone = $row['tsDone'];
    				if ($tsDone == 'done'){
    				echo "This account has been setup already."; 
    		}
    	} 
    }
    //END 
    
    


    Any simple pointers would be great, thanks!
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Part of your question is really about how PHP scopes variables... if you want to re-use the same variable name, I’d clear it before re-use, e.g.

      unset($results);
      $result = 'something new here';
      


      As for whether or not you need multiple queries... I’m not so good at reading the MODx database APIs... I usually write anything as MySQL code. What exactly are you trying to select? Chances are good that you can do it with one query... but sometimes it’s easier or necessary to use multiple queries.