Is it possible to connect to a database with tables in a database you are already connected with.
This is what I want :
I have a database with tables called regdbhost,regdbuser,regdbname and regdbtable. In the tables you can get the info to connect. For instance regdbhost=localhost, regdbuser=username, regdbname=database name.
This is my code
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false)
{
header('Location: index.php');
exit();
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=inschrijvingen.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('id','Name', 'Email', 'age','tekens'));
// fetch the data
$table = "user";
$query = mysql_query("SELECT regdbhost,regdbuser,regdbpass,regdbname,regdbtable FROM $table");
while($result = mysql_fetch_array($query))
{
$regdbhost = '.$result["regdbhost"].';
$regdbuser = '.$result["regdbuser"].';
$regdbpass = '.$result["regdbpass"].';
$regdbname = '.$result["regdbname"].';
$regdbtable = '.$result["regdbtable"].';
$link = mysql_connect($regdbhost, $regdbuser, $regdbpass);
mysql_set_charset('utf8',$link);
mysql_select_db($regdbname);
$rows = mysql_query('SELECT * FROM $regdbtable');
}
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows))
fputcsv($output, $row);
?>
I tried to connect it like this but it didnt work. What am I doing wrong?
Okay guys I fixed it.
I changed this :
$rows = mysql_query('SELECT * FROM $regdbtable');
to
$rows = mysql_query("SELECT * FROM $regdbtable");