Silahkan Melihat Tutorial di website kami dengan nyaman ENJOY YOUR LIFE ☕

Change connection root name, username, password and db name dynamically in PHP

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

<?php

$conn =  mysql_connect("localhost","root","");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error()
);
}
e
cho "Connected successfully";

$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);

/*
     By default 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_error();
           }
         
        }
     

?&gt
;

a
lso  i am create nice to meet you page it's file name thankyou.php
it's code is here

<?php
echo "Go back Check it your config are changed";
echo "</br&gt;";


?>

<a href="#" onclick="window.history.back();"&gt;Go Back

after create your main config file.

<?php
$conn =  mysql_connect("localhost","root","");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());



}


$


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'


];

ech
o "


</br>";
echo  $password = $row['password'];
 echo "</br&gt;";
 echo $database_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);
?>
dynamic connction
config

dowload and check code here
https://drive.google.com/folderview?id=0BwNaFGxzigBufkhqVFNMbC1hNFA3ZE1zTENjeVlHZTlaTk55dlgxR1k2UXBLTDFZYkFqX2s&usp=sharing









0 komentar:

Post a Comment

Change connection root name, username, password and db name dynamically in PHP