Change connection root name, username, password and deb name dynamically in PHP
Hello,
I am just creating dynamically change your dbname, servername, username and password.
You can create just one file you can change your DB.
Create database blog and create a connection table in PHP my admin
Here it's code and its data
Table structure for table `connction`CREATE TABLE IF NOT EXISTS
`connction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`localhost` varchar
(200) NOT NULL,
`username` varchar
(200) NOT NULL,
`password` varchar
(200) DEFAULT NULL,
`db_name` varchar
(200) NOT NULL,
PRIMARY KEY (
`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2
;Dumping data for table `connction`INSERT INTO
`connction` (
`id`,
`localhost`, `username`, `password`, `db_name`) VALUES
(1, '
localhost', 'root', '', 'blog');
Create dynamic_connection.php file here
it code
&
lt;?php$conn = mysql_conn
ect
("localhost","root","
");
if (
!$conn) {
die("Connection failed: "
. mysqli_connect_error
()
);
}
echo "Connected successfully";
$my_db = mysql_select
_db
("bl
og"
,$con
n);
if (
!$my_db) {
die ('Can\'t use b
log
: '
. mysql_er
ror
());
}
$my_sql_query = "select * fr
om connction";
$result = mysql_qu
ery
($my_sql_query);
$row = mysql_fetch_ar
ray
($result);
echo "</
br>";
p
rint_r
($row);
/*
By defaul
t get severname, username, password information. Change your database
Using below form.
*/
?><
form method="post">
<
table style="border: 2px solid;">
<
tr>
<
td>
<
label>
Enter Your
Localhost Name
</
label>
</
td>
<
td>
<
input type="text" name="txt_localhost_name" value="
php echo $row['localhost'] ?>" /> </
td>
</
tr>
<
tr>
<
td>
<
label>
Enter Your Username Name
</
label>
</
td>
<
td>
<
input type="text" name="txt_username_name" value="
php echo $row['username'] ?>" /> </
td>
</
tr>
<
tr>
<
td>
<
label>
Enter Your Password name
</
label>
</
td>
<
td>
<
input type="text" name="txt_password_name" value="
php echo $row['password'] ?>" /> </
td>
</
tr>
<
tr>
<
td>
<
label>
Enter Your database name
</
label>
</
td>
<
td>
<
input type="text" name="txt_database_name" value=
"php echo $row['db_name'] ?>" /> </
td>
</
tr>
<
tr>
<
td>
<
input type="submit" name="submit"/>
</
td>
</
tr>
</
table>
</
form>
<
?php if(isset
($_POST
['submit'])
){
$servername=$_POST
['txt_localhost_name'];
$username=$_POST
['txt_username_name'];
$password=$_POST
['txt_password_name'];
$database_name=$_POST
['txt_database_name'];
$sql = "UPDATE
connction SET localhost='$servername'
,username='$username'
,password='$password'
,db_name='$database_name' WHERE id=1";
$update_record = mysql_query
($sql);
if($update_record
){
echo "
Succesfull Update";
}else{ echo mysql_err
or();
}
}
?>
;
also i am create n
ice to meet you
page it's file name thankyou.php
it's code is here
<
?phpecho "Go back Check
it
your config ar
e changed";
echo "<
/br&
gt;";
?>
<
a href="#" onclick="window
.history
.ba
ck();">Go Back
after create your
main config file.
<
?php$conn = mysql_
connect
("lo
calhost"
,"root","");
if (
!$con
n) { die("Connection failed: "
. mysqli_connect_e
rror());
}
$my_db = mysql_select_db
("blog"
,$conn);
if (
!$my_db) {
die ('Can\'t use blog
: '
. mysql_error
());
}
$my_sql_query = "select * from
connction";
$result = mysql_query
($my_sql_query);
$row = mysql_fetch_array
($result);
echo "</
br>";
print_r
($row);
echo "</
br>";
echo $servername = $row
['
localhost'];
echo "</
br>";
echo $username = $row
['use
rna
me'
];
echo "
</
br>";
echo $pa
ssword = $row
['password'];
echo "</
br&
gt;";
echo $databas
e_name = $row
['db_name'];
$connection = mysql_connect
($servername, $username, $password
,true);
if (
!$connection) {
die("Connection failed: "
. mysql_error
());
}
$my_new_db = mysql_select_db
($database_name
,$connection);
$my_sql_query = "select * from
connction";
$result = mysql_query
($my_sql_query);
$row = mysql_fetch_array
($result);
echo "</
br>";
print_r
($row);
?>
dowload and check code here
https://drive.google.com/folderview?id=0BwNaFGxzigBufkhqVFNMbC1hNFA3ZE1zTENjeVlHZTlaTk55dlgxR1k2UXBLTDFZYkFqX2s&usp=sharing
0 komentar:
Post a Comment