Jump to content

Working on my install script, connectivity question


Eiolon

Recommended Posts

I have a install.php file that I created.  It asks for the MySQL user, pass, hostname and database they want to use.  When the script is run, the tables are created and the MySQL info is inserted into a connection table.

 

Now the tricky part.  On each page that needs to connect to the MySQL database, I have an include, which contains the following:

 

define ('DB_USER', ' ');
define ('DB_PASS', ' ');
define ('DB_HOST', ' ');
define ('DB_NAME', ' ');

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASS) OR die ('Cannot connect to MySQL server.');
mysql_select_db (DB_NAME) OR die ('Cannot connect to database.');

 

How do I tell the database connection script what the variables are from the connections table?  :o 

 

Yeah, I know I can manually put them in there, but the point of the script was to make it so the user does not have to go into the code.

Link to comment
Share on other sites

Instead of writing the code to the database, write it to a file.  Save the file as PHP and then include it into your other scripts.

 

<?php

if($_POST['from_site']) {

unset($_POST['from_site']);


$out_data="<?php
\$db_name='{$_POST['db_name_in']}';
\$table_prefix='{$_POST['table_prefix_in']}';
\$host='{$_POST['host_in']}';
\$user='{$_POST['user_in']}';
\$password='{$_POST['password_in']}';
?>";

$fp = fopen("./config.php", "w");
fwrite ($fp, $out_data);
fclose ($fp);


}

?>

<form action="" method="post">
<label for="host_in">Database Host:</label><input type="text" name="host_in" value="localhost" /><br />
<label for="db_name_in">Database Name:</label><input type="text" name="db_name_in" /><br />
<label for="table_prefix_in">Table Prefix:</label><input type="text" name="table_prefix_in" /><br />
<label for="user_in">User</label><input type="text" name="user_in" /><br />
<label for="password_in">Password</label><input type="password" name="password_in" /><br />
<br />
<input type="submit" name="from_site" value="Submit" />
</form>

 

Of course, you would need to save this to a directory that is above your public HTML, or is restricted from public view (.htaccess).

 

The reason this is the only viable alternative is that you cannot access a database without the database name, host, user, and password.  Therefore, logic states that you cannot retrieve these values from beyond that point.

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.