I've been learning a bit of PHP for a small school project. The project is almost done just that 1 thing is missing. I need a way to output something I searched into CSV / Excel.
I found a script which im using currently. It works beautifully I just can't figure out how to create a search form for it.


$DB_Server = "localhost"; //MySQL Server    
$DB_Username = "root"; //MySQL Username     
$DB_Password = "";             //MySQL Password     
$DB_DBName = "database";         //MySQL Database Name  
$DB_TBLName = "table"; //MySQL Table Name   
$filename = "search";         //File Name

//create MySQL connection 
$sql = "Select column1, column2 , ...  from $DB_TBLName";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database   
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());   
//execute query 
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
//end of printing column names  
//start while loop to get data
    while($row = mysql_fetch_row($result))
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
                $schema_insert .= "".$sep;
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print "\n";

Thats what im using currently. I want to be able to search, for example - ID1,ID2,ID3 and it outputs the search into a csv or excel file.
I really hope someone knows what im trying to do.
Thank you.

1 Year
Discussion Span
Last Post by rproffitt

I think you're trying to find code rather than design and write code. That's going to be trouble for you as I rarely see members here write apps for others (unless well paid.)

So, with that out of the way, it's time to head back to school on PHP. https://www.phpschool.io/ has been noted by many here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.