i am new to working in php, i am work on how to alter table dynamically in database through coding in php
database like this,
database name : data_switch
table name - data
did dataname host dbuser dbpwd dbname
1 abc local root root abc_db // here dataname create new database, when register new dataname
2 pqr ubuntu root passwd pqr_db
php code below:
<?php
$dsn = "localhost";
$username = "root";
$password = "passwd";
$db = new PDO("mysql:host=$dsn;dbname=data_switch", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach ($db->query("select * from data") as $row)
{
$dataname = $row['dataname'];
$host = $row['host'];
$dbuser = $row['dbuser'];
$dbpwd = $row['dbpwd'];
$dbname = $row['dbname'];
$connection_array['data1'][] = array(
'dataname' => $dataname,
'host' => $host,
'dbuser' => $dbuser,
'dbpwd' => $dbpwd,
'dbname' => $dbname
);
}
echo "<pre>";
print_r($connection_array);
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $dbuser, $dbpwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// here all dataname find but i dont know how to generate dataname's own connection. dynamically
?>
i am getting all dataname in loop but i am not able to dynamically diffrent connection,i am register new dataname in form as like abc then it create a new database dynamically, but when i am alter the table i am not getting how to connection dynamically on each dataname's host,dbname,dbpwd through each own dataname.
Any body having any idea please help to sort it out. Thanks
new PDO()call to different variables. Otherwise you always replace the prior one just as with wech other variable. Maybe you want to store the objects in an array... But as said: what you attempts is "strange"...