Jump to content

[SOLVED] mysql connection not seen in call to second php page


gchrt

Recommended Posts

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("\'","'"), "&#39;", $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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.