Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1415 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

MySQL Config Script

Thread title: MySQL Config Script
Closed Thread    
    Thread tools Search this thread Display Modes  
01-11-2006, 02:32 PM
#1
sketchie is offline sketchie
sketchie's Avatar
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
 
Posts: 835
iTrader: 1 / 100%
 

sketchie is on a distinguished road

  Old  MySQL Config Script

Ok so, say you've been making a script in PHP that needs access to MySQL, usually you'd get users to edit a file themselves (ie: config.php).

So I've made a small script that includes 3 files, install.php, config.php & readme.txt.

Install.php is what the user opens, and puts the MySQL information inside, the script them checks to makesure that config.php is writable (CHMOD: 777), tests the MySQL connection, tests the Database, then if it all works, writes the MySQL configuration to config.php.

config.php has to be CHMOD: 777, it stores the variables for MySQL, and also connects to MySQL for you, so all you have to do is use include("config.php"); to use MySQL in your own scripts.

readme.txt just explains to the user what they have to do.

config.php
PHP Code:
<?php

//Try Local host if nothing else works
$mysql_server "localhost";

//mysql username
$mysql_user "";

//mysql password
$mysql_pass "";

//mysql database
$mysql_db "";
?>
Install.php
PHP Code:
<html>
<head>
<title>MySQL Config Script</title>
<style type="text/css">
body
{
    font-family: verdana;
    background-color: #f0f0f0;    
}
a:link, a:visited
{
    color:#1c67ff;
    text-decoration: underline;
}
a:hover
{
    color:#5ab7ff;
    text-decoration: none;
}
.defaulttext
{
    font-size: 10px;
    color: #2f2f2f;
}
input.input
{
    width:150px;
    height:20px;
    background-color: #FFFFFF;
    color: #000000;
    font-size: 10px;
}
input.submit
{
    width:100px;
    height:20px;
    color: #000000;
    font-size: 10px;
}
.defaulttext
{
    font-size: 10px;
    color: #2f2f2f;
}
.bigtext
{
    font-size: 15px;
    color: #2f2f2f;
    font-weight: bolder;
}
</style>
</head>
<body>
<center>
<span class="bigtext">MySQL Config</span>
<div style="width:400px; border-width:1px; border-style: solid; border-color:#939393;">
<table cellpadding="0" cellspacing="0" border="0" width="400">
    <tr>
        <td valign="top" align="center">
        <?php
        
//Script created by Sketchie - Help from Bfsog.
        
        
echo '<span class="defaulttext">';
        
        if (
$_POST['submit']=="submit")
        {
            
$file "config.php";
            
$mysql_server addslashes($_POST['mysql_server']);
            
$mysql_user addslashes($_POST['mysql_user']);
            
$mysql_pass addslashes($_POST['mysql_pass']);
            
$mysql_db addslashes($_POST['mysql_db']);
            
            if (
is_writable($file)) {
                
$dbcx mysql_connect($mysql_server$mysql_user$mysql_pass);
                if(
$dbcx)
                {
                    
$dbc mysql_select_db($mysql_db$dbcx);
                    if(
$dbc)
                    {
                        
$ferror TRUE;
                        
$write "<?php \$mysql_server=\"$mysql_server\"; \$mysql_user=\"$mysql_user\"; \$mysql_pass=\"$mysql_pass\"; \$mysql_db=\"$mysql_db\"; ?><?php \$dbcx = mysql_connect(\$mysql_server, \$mysql_user, \$mysql_pass); ?><?php mysql_select_db(\$mysql_db, \$dbcx); ?>";
                        if (!
$handle fopen($file'w'))
                        {
                             echo 
"<span style=\"color:#ff0000\">Cannot open file ($file).</span>";
                             
$ferror FALSE;
                             
$show TRUE;
                        }
                        if (
fwrite($handle$write) === FALSE)
                        {
                            echo 
"<span style=\"color:#ff0000\">Cannot write to file ($file).</span>";
                            
$ferror FALSE;
                            
$show TRUE;
                        }
                        
                        if (
$ferror)
                        {
                        echo 
"MySQL data was sucessfully added to ($file).<br/><br/>Please Delete install.php";
                        }
                    }
                    else 
                    {
                        echo 
'<span style="color:#ff0000">Could not connect to MySQL Database, Please makesure you entered the right database name.</span>';
                        
$show TRUE;
                    }
                
mysql_close($dbcx);
                }
                else 
                {
                    echo 
'<span style="color:#ff0000">Could not connect to MySQL, Please enter your settings correctly.</span>';
                    
$show TRUE;
                }
                
            } else {
                echo 
'<span style="color:#ff0000">The config.php is not writable, please set chmodd on config.php to 777 or edit it manually.</span>';
                
$show TRUE;
            }
        }
        
        if (
$show || empty($_POST['submit']))
        {
            echo 
'<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
            echo 
'<b>MySQL Settings</b><br/>';
            echo 
'Server:<br/>(If you don\'t know try localhost or ask your host).<br/>';
            echo 
'<input type="text" name="mysql_server" value="localhost" class="input" /><br/><br/>';
        
            echo 
'Username:<br/>';
            echo 
'<input type="text" name="mysql_user" value="'.$mysql_user.'" class="input" /><br/><br/>';
        
            echo 
'Password:<br/>';
            echo 
'<input type="password" name="mysql_pass" value="" class="input" /><br/><br/>';
        
            echo 
'Database:<br/>';
            echo 
'<input type="text" name="mysql_db" value="'.$mysql_db.'" class="input" /><br/><br/><br/>';
        
            echo 
'<input type="submit" name="submit" value="submit" />';
            echo 
'</form>';
        }
        echo 
'</span>';
        
?>
        </td>
    </tr>
</table>
</div>
</center>
</body>
</html>
readme.txt
Code:
Upload Install.php and Config.php to your chosen folder on your website.

In your FTP, CHMOD config.php to 777 (so install.php can write to it). 

Open up install.php in your web browser and fill in your MySQL details. 


The script will then auto detect whether your MySQL information is correct (and whether config.php is writable).

After sucessfull installation delete install.php.


HELP
----
If you can't CHMOD, edit config.php manually.

CREDITS
-------
MySQL Config Script created by Sketchie - with help from bfsog.
What do you think?

01-12-2006, 01:05 AM
#2
xZaft is offline xZaft
Status: Member
Join date: Jul 2005
Location: Massachusetts, US
Expertise:
Software:
 
Posts: 428
iTrader: 0 / 0%
 

xZaft is on a distinguished road

  Old

Good, just one thing, why do you stop then start the php code that many times?!

01-12-2006, 01:24 AM
#3
Nirvana- is offline Nirvana-
Status: Member
Join date: Sep 2005
Location: United States
Expertise:
Software:
 
Posts: 286
iTrader: 0 / 0%
 

Nirvana- is on a distinguished road

  Old

Originally Posted by xZaft
Good, just one thing, why do you stop then start the php code that many times?!
He only did that around 3-4 times...

Anyways, it looks like it would work, that would come in very handy, someday. Thanks for sharing it though!

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed