We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32407
    • 35 Posts
    Hi I am trying to make a sql call to another data base using a snippet, but it does not return a value even though there is a value.

    I used a static php page and it does return the value but when I put it into modx nothing returns.

    $database_server = ’’;
    $database_user = ’’; //Both user and name are the same for consistency.
    $database_password = ’’;
    $baseurl = ’[[++site_url:stripString=`http://`:stripString=`www.`:stripString=`/`:stripString=`modx.`]]’;
    $con = mysql_connect($database_server,$database_user,$database_password);
    mysql_select_db($database_user, $con);
    $keys = mysql_query("select apikey from googleapikeys where domain=’$baseurl’");
    while ($row = mysql_fetch_assoc($keys))
    {
    return $baseurl;
    }
    mysql_close($con);

    please help someone.
      • 22303 MODX Staff
      • 10,725 Posts
      Umm, what is this???
      $baseurl = '[[++site_url:stripString=`http://`:stripString=`www.`:stripString=`/`:stripString=`modx.`]]';

      That won’t be of any use to your query since it won’t be processed until after you return the content to the parser. What are you trying to get a value for with this tag? Maybe you are looking for:
      $modx->getOption('base_url');

      huh
        • 32407
        • 35 Posts
        I have changed my code to this

        	
        	$baseurl = $modx->getOption('site_url');
        	$baseurl = str_replace("/","",str_replace("modx.","",str_replace("www.","",$baseurl)));
        	$con = mysql_connect($database_server,$database_user,$database_password);
        	mysql_select_db($database_user, $con);
        	$keys = mysql_query("select apikey from googleapikeys where domain='$baseurl'");
        	while ($row = mysql_fetch_assoc($keys))
        	{
        		return $row[apikey];
        	}
        	mysql_close($con);	


        and it still does not return a value
          • 18913
          • 654 Posts
          mysql_select_db($database_user, $con);


          Shouldn’t the parameter be the database name and not the user? How about echoing the results after each line so you can see where it’s going wrong?
          Matt
            • 32407
            • 35 Posts
            I got it working I forgot to add the replace for the http:

            	$baseurl = $modx->getOption('site_url');
            	$baseurl = str_replace("/","",str_replace("modx.","",str_replace("www.","",str_replace("http://","",$baseurl))));
            	$con = mysql_connect($database_server,$database_user,$database_password);
            	mysql_select_db($database_user, $con);
            	$keys = mysql_query("select apikey from googleapikeys where domain='$baseurl'");
            	while ($row = mysql_fetch_assoc($keys))
            	{
            		return $row[apikey];
            	}
            	mysql_close($con);