Ok,
Code above gives "connected successfully" message. So, that helps to see that I CAN connect to the database. However, I don't understand why my php script won't connect. It works fine on my server. I have created a database with the correct name and a user with the correct name and privileges. I've tried using the script with the user I created and also with "root" and no password. Either way, no go.
Here's a copy of the actual code of the file (where I've anonymized my cookie_host, database_username, database_user_password and database_name):
I've checked to make sure that the username, password and database name that I've used in this file match up with those that I created.
To reiterate, this file works fine on my server. I just wanted to have a test server where I could test new php pages without always having to upload them for testing. Below the code below I have also pasted what I see in the browser when I try to load index.php, which references cookiemonster.php (the file which contains the code below).
======================================================
define("MK__COOKIE_HOST", ".mydomain.com"
;
define("MK__COOKIE_URL", "ref"
;
define("MK__DATABASE_HOST", "localhost"
;
define("MK__DATABASE_USERNAME", "user_e1071"
;
define("MK__DATABASE_PASSWORD", "user-password"
;
define("MK__DATABASE_NAME", "user_e1071"
;
define("MK__DATABASE_TABLE", "form_23"
;
class Database
{
function Database()
{
$tmp_dblink = @mysql_pconnect(MK__DATABASE_HOST, MK__DATABASE_USERNAME, MK__DATABASE_PASSWORD);
if (!$tmp_dblink)
{
echo "<HTML>\n";
echo "<HEAD>\n";
echo "<TITLE>HomeBizHit.com - Database Connection Error</TITLE>\n";
echo "</HEAD>\n";
echo "<BODY><TABLE HEIGHT=\"50%\" WIDTH=\"100%\" BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\"><TR><TD VALIGN=\"MIDDLE\" ALIGN=\"CENTER\">\n";
echo "<DIV ALIGN=\"CENTER\" STYLE=\"font-family: monospace; font-size: 11pt; color: maroon\"><B>[ HomeBizHit.com ]</B></DIV><BR>";
echo "<DIV ALIGN=\"CENTER\" STYLE=\"font-family: Tahoma, Arial; font-size: 9pt; color: black\"><B>Fatal Error</B>: Unable to open socket to database server.</DIV>\n";
echo "</TD></TR></TABLE></BODY>\n";
echo "</HTML>\n";
exit();
}
}
function Query($theQuery)
{
mysql_select_db(MK__DATABASE_NAME);
return(mysql_query($theQuery));
}
function NumRows($theResult)
{
return(mysql_num_rows($theResult));
}
function FetchArray($theResult)
{
return(mysql_fetch_array($theResult));
}
function FreeResult($theResult)
{
mysql_free_result($theResult);
}
}
$Database = new Database;
class Affiliate
{
var $AffiliateId;
function Affiliate()
{
global $Database;
if (isset($_GET[MK__COOKIE_URL]))
{
$result = $Database->Query("SELECT * FROM ".MK__DATABASE_TABLE." WHERE affiliate_id = '".$_GET[MK__COOKIE_URL]."';"
;
if (!$Database->NumRows($result))
{
$this->AffiliateId = $this->RandomGenerator();
$this->SetCookies($this->AffiliateId, TRUE);
} else
{
if (isset($_COOKIE["MKAFFILIATE"][1]))
{
if (isset($_COOKIE["MKAFFILIATE"][2]))
{
$this->AffiliateId = $_GET[MK__COOKIE_URL];
$this->SetCookies($this->AffiliateId, FALSE);
} else
{
if ($_COOKIE["MKAFFILIATE"][1] == $_GET[MK__COOKIE_URL])
{
$this->AffiliateId = $_GET[MK__COOKIE_URL];
$this->SetCookies($this->AffiliateId, FALSE);
} else
{
$this->AffiliateId = $_COOKIE["MKAFFILIATE"][1];
$this->SetCookies($this->AffiliateId, FALSE);
}
}
} else
{
$this->AffiliateId = $_GET[MK__COOKIE_URL];
$this->SetCookies($this->AffiliateId, FALSE);
}
}
} else
{
if (isset($_COOKIE["MKAFFILIATE"][1]))
{
$this->AffiliateId = $_COOKIE["MKAFFILIATE"][1];
if (isset($_COOKIE["MKAFFILIATE"][2]))
{
$this->SetCookies($this->AffiliateId, TRUE);
} else
{
$this->SetCookies($this->AffiliateId, FALSE);
}
} else
{
$this->AffiliateId = $this->RandomGenerator();
$this->SetCookies($this->AffiliateId, TRUE);
}
}
}
function RandomGenerator()
{
global $Database;
$tmpTotal = 0;
$result = $Database->Query("SELECT * FROM ".MK__DATABASE_TABLE." ORDER BY affiliate_id;"
;
while ($row = $Database->FetchArray($result))
{
for ($tmpCount = 1; $tmpCount <= $row["affiliate_weight"]; $tmpCount++)
{
$tmpArray[] = $row["affiliate_id"];
$tmpTotal++;
}
}
$Database->FreeResult($result);
shuffle($tmpArray);
$tmpAffiliate = rand(0, ($tmpTotal - 1));
return($tmpArray[$tmpAffiliate]);
}
function SetCookies($theAffiliate, $theRandomGenerator = FALSE)
{
setcookie("MKAFFILIATE[1]", $theAffiliate, time() + (60 * 60 * 24 * 363), "/", MK__COOKIE_HOST);
if ($theRandomGenerator == TRUE)
{
setcookie("MKAFFILIATE[2]", $theAffiliate, time() + (60 * 60 * 24 * 363), "/", MK__COOKIE_HOST);
} else
{
if (isset($_COOKIE["MKAFFILIATE"][2]))
{
setcookie("MKAFFILIATE[2]", $theAffiliate, time() - (60 * 60 * 24 * 363), "/", MK__COOKIE_HOST);
}
}
}
function Data($theField)
{
global $Database;
$result = $Database->Query("SELECT * FROM ".MK__DATABASE_TABLE." WHERE affiliate_id = '".$this->AffiliateId."';"
;
if (!$Database->NumRows($result))
{
return("Unknown Affiliate"
;
} else
{
while ($row = $Database->FetchArray($result))
{
$tmpValue = $row["affiliate_".strtolower($theField)];
}
$Database->FreeResult($result);
return($tmpValue);
}
}
}
$Affiliate = new Affiliate;
====================================================
This is what I see in the browser when I try to load my index.php file which has cookiemonster.php as an include
=====================================================
[ HomeBizHit.com ]
Fatal Error: Unable to open socket to database server.
======================================================
Edited 1 time(s). Last edit at 10/05/2008 09:14PM by klawncare1239.