Hello everyone. I'm working on a new install script for an open source project I'm working on, and for the life of me I'm having a problem with my mysql_select_db that I'm just too tired to see. I'm hoping another well rested person can help. -laff- Anywho, the database name is passed through a web form on another page. I've checked all the variables, all the webserver information is correct both before the connectToDatabase function is called and while inside the function. The connection to the database is established fine also. So the exact problem im having is when trying to actually select the database with the mysql_select_db function in connectToDatabase function is this. It dies directly after the select (with the or die), and reports 'No Database Selected'. I've tried using quotes, no quotes, upside down and backwards. Heh. Im sure its something simple I'm overlooking. Thanks for the help!
<?
// makeInstall.php--
// Last Revision: 12.06.2003--
function tableExists($tableName){
global $databaseName;
$tableExistance=0;
$confirmTableExists = @mysql_list_tables($databaseName);
while ($hTableCheck=@mysql_tablename($confirmTableExists, $i++)) {
if($hTableCheck==$tableName) { $tableExistance=1; }
}
if($tableExistance==1) {
return TRUE;
} else {
return FALSE;
}
}
function loadURL($wwwAddress) {
@header("Location: ".$wwwAddress);
}
// The array passed to this function should reflect the
// tables in the following order:
// brands,categories,customers,items,sales,users
function createTables($arrayOfTableNames) {
// SQL statement here was removed to post message--
$installSQL =explode (';' ,$tableCreationQuery );
foreach( $installSQL as $createThisTable ) {
mysql_query($createThisTable) or die(mysql_error());
}
if($failedTables>0) {
return FALSE;
} else {
return TRUE;
}
}
function installSuccessful() {
reportError("install_successful");
}
function reportError($errorType) {
switch($errorType) {
case table_exists:
echo "
<html><head><title>Install Error</title></head><body>
<form method=\"POST\" action=\"makeinstall.php\"><p align=\"center\">
<img border=\"0\" src=\"../errors/install/table_exists.gif\" width=\"436\" height=\"90\"><br>
<br><b><font face=\"Verdana\" size=\"2\">What would you like to do to fix this
problem?</font></b><br><br><input type=\"submit\" value=\"Use Table Prefix (Recommended)\" name=\"errorButUsePrefix\"><br>
<br><input type=\"submit\" value=\"Overwrite Existing Tables\" name=\"errorButOverwrite\"><br>
</p></form></body></html>";
exit;
case create_tables:
echo "<html><head><title>Install Error</title></head>
<body><p align=center><br><br><img border=0 src=\"../errors/install/table_error.gif\" width=436 height=90><br>
<br><font face=Verdana size=2><a href=javascript:history.back(1);><font color=#000000>Click
Here To Continue</font></a></font></p></body></html>";
exit;
case database_connection:
echo "<html><head><title>Install Error</title></head>
<body><p align=center><br><br><img border=0 src=\"../errors/install/database_connection.gif\" width=436 height=90><br>
<br><font face=Verdana size=2><a href=javascript:history.back(1);><font color=#000000>Click
Here To Continue</font></a></font></p></body></html>";
exit;
case install_successful:
loadURL("../login.php");
exit;
}
}
function reportFieldMissing($fieldName) {
echo "The form field $fieldName is missing. Please try again.";
exit;
}
function writeSettingsFile($company,$address,$phone,$email,$server,$database,$username,$password,$arrayOfTableNames) {
$writeConfigurationFile="<?
error_reporting (E_ERROR | E_WARNING | E_PARSE);
\$company=\"$company\";
\$address=\"$address\";
\$phone=\"$phone\";
\$email=\"$email\";
\$server=\"$server\";
\$database=\"$database\";
\$username=\"$username\";
\$password=\"$password\";
\$tableBrands=\"$arrayOfTableNames[0]\";
\$tableCategories=\"$arrayOfTableNames[1]\";
\$tableCustomers=\"$arrayOfTableNames[2]\";
\$tableItems=\"$arrayOfTableNames[3]\";
\$tableSales=\"$arrayOfTableNames[4]\";
\$tableUsers=\"$arrayOfTableNames[5]\";
?>";
$hWriteConfiguration = fopen( "../settings.php", "w+" ) or die ( "Operation Failed!" );
fputs( $hWriteConfiguration, "$info" );
fclose( $hWriteConfiguration );
}
function connectToDatabase() {
global $databaseServer;
global $databaseUsername;
global $databasePassword;
global $databaseName;
// More debugging, both return the same data--
echo "in header<br>";
echo $databaseServer."<br>";
echo $databaseUsername."<br>";
echo $databasePassword."<br>";
echo $databaseName."<br>";
$hDatabaseCnx=mysql_connect($databaseServer,$databaseUsername,$databasePassword) or die(mysql_error());
// This is where we mess up, it dies & reports No Database Selected--
$hSelectDB=mysql_select_db($dbVariable) or die(mysql_error());
return TRUE;
}
// ------------------------ Install Process Begins Here ----------------------- //
// Table configuration--
// In order for the install to work correctly please
// do not modify the table order. Feel free to change
// the table names however.
$tablePrefixForInstall="ps_";
$installTable[0]="brands";
$installTable[1]="categories";
$installTable[2]="customers";
$installTable[3]="items";
$installTable[4]="sales";
$installTable[5]="users";
if(isset($_POST[installPOS])) {
$companyName=$_POST[companyName];
$companyAddress=$_POST[companyAddress];
$companyPhone=$_POST[companyPhone];
$companyEmail=$_POST[companyEmail];
$databaseServer=$_POST[databaseServer];
$databaseName=$_POST[databaseName];
$databaseUsername=$_POST[databaseUsername];
$databasePassword=$_POST[databasePassword];
if($companyName=="") {
reportFieldMissing("Company Name");
} elseif($companyPhone=="") {
reportFieldMissing("Company Phone Number");
} elseif($databaseServer=="") {
reportFieldMissing("Database Server");
} elseif($databaseName=="") {
reportFieldMissing("Database Name");
} elseif($databaseUsername=="") {
reportFieldMissing("Database Username");
} elseif($databasePassword=="") {
reportFieldMissing("Database Password");
}
// Debugging--
echo "no error entry point";
echo $databaseServer."<br>";
echo $databaseUsername."<br>";
echo $databasePassword."<br>";
echo $databaseName."<br>";
if(connectToDatabase()) {
for($verifyTables=0;$verifyTables<count($installTable);$verifyTables++) {
if(tableExists($installTable[$verifyTables])) {
reportError("table_exists");
}
}
if(createTables($installTable,$databaseName)) {
writeSettingsFile($companyName,$companyAddress,$companyPhone,$companyEmail,$databaseServer,$databaseName,$databaseUsername,$databasePassword,$installTable);
} else {
reportError("create_tables");
}
} else {
reportError("database_connection");
}
installSuccessful();
} elseif(isset($_POST[errorButOverwrite])) {
echo "error 1 entry point";
if(connectToDatabase()) {
if(createTables($installTable,$databaseName)) {
writeSettingsFile($companyName,$companyAddress,$companyPhone,$companyEmail,$databaseServer,$databaseName,$databaseUsername,$databasePassword,$installTable);
} else {
reportError("create_tables");
}
installSuccessful();
}
} elseif(isset($_POST[errorButUsePrefix])) {
echo "error 2 entry point";
if(connectToDatabase()) {
for($useTablePrefix=0;$useTablePrefix<count($installTable);$useTablePrefix++) {
$installTable[$useTablePrefix]="ps_".$installTable[$useTablePrefix];
}
if(createTables($installTable,$databaseName)) {
writeSettingsFile($companyName,$companyAddress,$companyPhone,$companyEmail,$databaseServer,$databaseName,$databaseUsername,$databasePassword,$installTable);
} else {
reportError("create_tables");
}
installSuccessful();
}
}
// ------------------------- End Of Install Process ------------------------------
?>