Eiolon Posted March 5, 2011 Share Posted March 5, 2011 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/229718-working-on-my-install-script-connectivity-question/ Share on other sites More sharing options...
jcbones Posted March 6, 2011 Share Posted March 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229718-working-on-my-install-script-connectivity-question/#findComment-1183416 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.