We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6513
    • 28 Posts
    Hello all.
    I am trying to execute a snippet that has two queries in it. I am still learning php and mysql so there may be a better way to accomplish this.


    here is my snippet code
    
    $output = '';//create variable for holding the output
    $sql = $modx->db->query( 'SELECT cat_bespoke.co_name,main_info.co_name,main_info.city,main_info.country,main_info.logo_large,main_info.recommend,main_info.type FROM main_info,cat_bespoke WHERE cat_bespoke.co_name=main_info.co_name ORDER BY main_info.co_name ASC');
    $resultArray = $modx->db->makeArray( $sql );//put it into an array
    foreach($resultArray as $item)//go through each record in the array
    {//start looping through the data array
    $co = $item['co_name'];//to be used in second query for a random image form the company designated by $item['co_name']
    $cFolder= $item['co_name'];
    $cFolder= str_replace('&', 'and', $ciFolder);
    $cFolder= str_replace(' ', '', $ciFolder);
    $cFolder= str_replace('\'', '', $ciFolder);
    $params['cPath']= $cFolder;
    $params['iPath'] = 'assets/images/';
    $params['company']=$item['co_name'];//set the placeholders, the $params sets the name of the placeholder, the $item is the column name from the DB table
    $params['city']=$item['city'];
    $params['country']=$item['country'];
    $params['logo']=$item['logo_large'];//set the placeholders, the $params sets the name of the placeholder, the $item is the column name from the DB table
    $params['rec']=$item['recommend'];
    $params['btype']=$item['type'];
      $q = $modx->db->query('SELECT images.small_img,images.alt WHERE images.co_name='.$co.' ORDER BY Rand()');
      $resultArray = $modx->db->makeArray($q);
      foreach($resultArray as $image)
      {
      $params['img']=$image['small_img'];
      $params['alt']=$image['alt'];
      }
    $output.=$modx->parseChunk('companyListTpl', $params, '[+', '+]');//define the chunk name and process the content, here it's ProjectTpl which has the placeholders for the data
    }//finish looping through the data array
    return $output;//now return the output from the processed chunk
    


    I get an error that states the following: " Execution of a query to the database failed - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ’WHERE images.co_name=Axfords ORDER BY Rand()’ at line 1 "

    Axfords is the first company in this particular category - designated by the $co variable. The site is a collection of information on various lingerie companies/designers.

    I am not sure why but anytime I use a variable in a modx query in a WHERE clause I get an error. I have searched the documentation and the forums but to no avail. Not sure what I am missing but I am sure it is a newbie mistake.

    A point in the right direction would be greatly appreciated smiley
      • 4310
      • 2,310 Posts
      You don’t seem to have FROM `images` in your second query
        • 6513
        • 28 Posts
        I cannot believe I missed that laugh

        Thanx!

        EDIT
        ########################################

        After adding the missing "FROM" statement I get this error: "Execution of a query to the database failed - Unknown column ’Axfords’ in ’where clause’"

        Why does the system recognize ’Axfords’ as a column and not a value of a column in a WHERE statement? I can make these statements outside of modx and get the results I need. I have to be missing something very small....

        Thanx for the help!!
          • 4310
          • 2,310 Posts
          Maybe try :
          WHERE images.co_name=''.$co.''
            • 6513
            • 28 Posts
            No luck again.

            I even tried setting up a ’standard’ db connection but it did not work either.$hostname,$username,$password,$database etc......
            Tried separating the Tables for my content into another DB and accessing it but it did not work either.

            I am using a WAMP server here at home, could that be the issue? I read some information about IIS servers and how they are tricky. My WAMP server is a self installing full package so I did not do anything with IIS at home.

            I may transfer all of this to my Dev server which is Linux and see if that helps but I think it might be a long shot.

            thanx,
            Daniel
              • 10487 MODX Staff
              • 1,535 Posts
              Quote from: bunk58 at Jan 06, 2010, 09:21 AM

              Maybe try :
              WHERE images.co_name=\''.$co.'\'

              Nearly tongue The quotes need to be escaped.
                Garry Nutting
                Senior Developer
                MODX, LLC

                Email: [email protected]
                Twitter: @garryn
                Web: modx.com
                • 6513
                • 28 Posts
                Awesome! Works perfectly smiley

                Completely forgot about escaping.

                Thanx!

                How do I make sure this forum post gets closed as SOLVED?