gchrt Posted July 24, 2009 Share Posted July 24, 2009 Hi, This is my first page: <?php require 'load2golfersmartdb.php'; $hostname_amem = "***********"; $mysqlUser_amem = "*********"; $mysqlPW_amem = "********"; $conn_amem = mysql_connect($hostname_amem, $mysqlUser_amem, $mysqlPW_amem, TRUE) or die ("Error connecting to mysql " .mysql_error()); $gsdb_amem = "gsamember"; mysql_select_db($gsdb_amem, $conn_amem) or die("Unable to select database " .mysql_error()); $mysQ = mysql_query("SELECT * FROM gsamember_members", $conn_amem); while ($row = mysql_fetch_array($mysQ)){ $data = unserialize($row['data']); $country = $data['memcountry']; if(is_array($country)) { $memcntry = $country[0]; }else { $memcntry = $country; } $row['memcountry'] = $memcntry; $sentInfo = load2gmart($row); print_r($sentInfo); echo "<br><br>"; } mysql_close($conn_amem); mysql_close($conn_gsmart); ?> The load2golfersmartdb.php : <?php require 'connectdb_gsMart.php'; function getCountryNum($str) { $queryStr = "SELECT * FROM CubeCart_iso_countries WHERE printable_name='" .$str. "';"; $queryRes = mysql_query($queryStr, $conn_gsmart); $row = mysql_fetch_array($queryRes); $countryNum = $row['id']; mysql_free_result($queryRes, $conn_gsmart); return $countryNum; } function mySQLSafe($value, $quote = "'") { ## Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } ## Strip quotes if already in $value = str_replace(array("\'","'"), "'", $value); ## Quote value if (function_exists('mysql_real_escape_string')) { $value = mysql_real_escape_string($value); } else { $value = mysql_escape_string($value); } $value = $quote . trim($value) . $quote; return $value; } function load2gmart($amemUserInfo){ $record["email"] = mySQLSafe($amemUserInfo['email']); $record["title"] = mySQLSafe($amemUserInfo['title']); $record["firstName"] = mySQLSafe($amemUserInfo['name_f']); $record["lastName"] = mySQLSafe($amemUserInfo['name_l']); $record["companyName"] = mySQLSafe($amemUserInfo['companyName']); $record["add_1"] = mySQLSafe($amemUserInfo['staddress']); $record["add_2"] = mySQLSafe($amemUserInfo['add_2']); $record["town"] = mySQLSafe($amemUserInfo['memcity']); $record["county"] = mySQLSafe($amemUserInfo['memstate']); $record["postcode"] = mySQLSafe($amemUserInfo['memzip']); // one connection with amember registration //$country1 = $amemUserInfo['memcountry']; //$memcountry = $country1[0]; //$record["country"] = mySQLSafe(getCountryNum($memcountry)); // transfer all records $icountry = $amemUserInfo['memcountry']; if($icountry != "") { $record["country"] = mySQLSafe(getCountryNum($icountry)); }else { $record["country"] = ""; } $record["phone"] = mySQLSafe($amemUserInfo['phone']); $record["mobile"] = mySQLSafe($amemUserInfo['mobile']); $record["ipAddress"] = mySQLSafe($amemUserInfo['remote_addr']); $record["regTime"] = mySQLSafe(time()); $record["type"] = 1; if(isset($amemUserInfo['optIn1st'])){ $record["optIn1st"] = mySQLSafe($amemUserInfo['optIn1st']); } $salt = randomPass(6); $record["salt"] = "'".$salt."'"; $record["password"] = mySQLSafe(md5(md5($salt).md5($amemUserInfo['pass0']))); $record["ems_group_id"] = 2; return $record; //$insert = insert("CubeCart_customer", $record); return FALSE; } ?> The required file at top of load2golfersmartdb.php is the mysql connection ($conn_gsmart) to a separate server. I checked, when in transferAmem-gsmart.php $conn_gsmart is live. When sent to the function on load2... the connection has been lost. Not sure why. Quote Link to comment Share on other sites More sharing options...
btherl Posted July 24, 2009 Share Posted July 24, 2009 What is transferAmem-gsmart.php? Quote Link to comment Share on other sites More sharing options...
gchrt Posted July 24, 2009 Author Share Posted July 24, 2009 I apologize. The first page of code is transferAmem-gsmart.php. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2009 Share Posted July 24, 2009 You are attempting to use $conn_amem inside of a function, where it does not exist. You need to pass it into the function as a parameter when you call the function or you need to use a class. Quote Link to comment Share on other sites More sharing options...
gchrt Posted July 24, 2009 Author Share Posted July 24, 2009 It is actually the $conn_gsmart that is created at the top of the load2gs... file that is not being seen. I thought if one set a file require at the top of the page that it would be globally available to all functions on that page. I was successfully using this page with another file but since I had to start another mysql connection (conn_amem) I needed to make a distinction of the two and now I've lost the mysql connection (conn_gsmart) for this page (load2gs...). I realize there is something amiss about variable availability but I'm just not seeing it. Quote Link to comment Share on other sites More sharing options...
vineld Posted July 24, 2009 Share Posted July 24, 2009 It does not matter if it is created in the actual file, global variables are not accessible in the functions unless you declare them. However, in case you only have one database connection there is really no need to use the connection variable. If you wish to have functions access global variables you will have to do it this way: $myVariable = 6; function myFunction() { global $myVariable; // Now I can use $myVariable here as well; } It would also be wise to store the database connection in a separate file which you include since you will normally use the connection in many various files. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2009 Share Posted July 24, 2009 Don't suggest using the global keyword to being a value into a function. In this case, the OP is dealing with more than one database connection. By using the global keyword instead of passing the database connection in as a parameter in the function call, you hard-code that function so that it can only operate on one specific database connection. This breaks the general purpose nature of a function, in which case you might as well not be using a function at all. Quote Link to comment Share on other sites More sharing options...
vineld Posted July 24, 2009 Share Posted July 24, 2009 Maybe I was a bit quick there. Since you had already pointed him into the right direction I was only commenting on his last post regarding the variable not being accessible within the function. I did not mean to imply that it was a good solution in this case although I don't agree with you that there is never any point in accessing global variables from within a function but perhaps that was not what you meant either Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.