Here is the code I have came up for this problem.
$link = mysql_connect($host, $user, $pass) or die("Can not connect to the host." . mysql_error());
mysql_select_db($db) or die("Can not connect to the particular database.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "
";
$values = mysql_query("SELECT * FROM ".$table." WHERE forwarded_oce='1'");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
$smallerid = $rowr[$i-1];
$smaller = mysql_query("SELECT budget FROM `db`.`table` WHERE id='$smallerid'");
}
$csv_output .= $smaller;
$csv_output .= "
";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
So, the issue is that when I try to add the extra column with additional information from a different mysql table it prints out blank. I cannot seem to find my mistake in the code, but maybe somebody knows, or have expierence with these tasks to at least point me in the right direction.
What I am trying to do is - combine data from 2 different MySQL tables, and identifier for the data selected from the second table comes from the last column of the first table.
If you know any framework with could help me solve this issues it would be perfect. I am banging my head against the wall with this problem for 2 days allready and official PHP documentation do not have problems like this in mysql_query section. ..... Thanks!!!!!
I am not sure what you are trying to do , but it seems like you want to take the last columns value and use it for finding a value from another table and add that to the record. For that i think something like this should work:
$link = mysql_connect($host, $user, $pass) or die("Can not connect to the host." . mysql_error());
mysql_select_db($db) or die("Can not connect to the particular database.");
$csv_output = "";
$last_col = "";
$values = mysql_query("SELECT * FROM ".$table." WHERE forwarded_oce='1'");
while ($rowr = mysql_fetch_assoc($values)) {
if(!$csv_output){
foreach($rowr as $fld_name => $fld_val){
$csv_output .= $fld_name.", ";
$last_col = $fld_name;
}
$csv_output .= "New_Field
";
}
$smallerid = $rowr[$last_col];
list($smaller) = mysql_fetch_row(mysql_query("SELECT budget FROM `db`.`table` WHERE id = '$smallerid'"));
$rowr[] = $smaller;
$csv_output .= implode(', ' , $rowr) ;
$csv_output .= "
";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
you use
mysql_query
but you do not get the results row like :
mysql_result or mysql_fetch_row
add after the query line another line like:
$smaller = mysql_query("SELECT budget FROM `db`.`table` WHERE id='$smallerid'");
$smaller = mysql_fetch_row($smaller);