WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
bastien koert
Date : Jul 23rd 2004
Grade : 2 of 5 (graded 5 times)
Viewed : 19659
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

Hi All,

I'm sure its been done to death, but here it is. Simple and it allows input of a table name for the user to export...


Turn off all error reporting
error_reporting(0);

db conn stuff
mysql_connect('host', 'user', 'passwrd') or die ("Error connecting to Database");

<?php
if (!$_POST['submit']){
   
show_form();
}else{
   
$table = @$_POST['mytable'];
   
    if (!
eregi("[a-z0-9_]{1,64}",$table)){
       
show_form();
        die(
"disallowed_table name. if your tables names contain other characters, consider renaming them.");
  }
//end if       

   
require("local_conn.php");
   
$sql = "select * from $table ";
   
    if (!(
$conn2=mysql_connect($host, $username, $pwd)))  {
       
printf("error connecting to DB by user = $username and pwd=$pwd");
        exit();
    }
   
$db2=mysql_select_db($dbname,$conn2) or die("Unable to connect to local database
   
   
$result = mysql_query($sql) or die ("Can't complete query because ".mysql_error());
    if (($result)&&(mysql_num_rows($result)>1)){
        return $result;
    }else{
        echo "No Results found. Check your table name";
        die();   
    }
    if (($result) && (mysql_num_rows($result)>0)){
       
        //set the path and file name
        //$dir="/path/to/file/";
        $filename = '
test.txt';
   
       if (!$handle = fopen($filename, '
w')) {
             echo "Cannot open file ($filename)";
             exit;
       }
   
        while ($rows = mysql_fetch_array($result)){
            $fields = mysql_num_fields($result);
            $content = "";
            //calc the length of the line (number of elements)
                for($x = 0; $x < $fields-1; $x++){
   
                    $content .= "" . $rows[$x] . " | ";
               
                }//next
   
          $content = substr($content, 0, strlen($content)-2). "\n"; //remove the last pipe as its not needed and adds the new line character
   
               // Write $content to our opened file.
               if (fwrite($handle, $content) === FALSE) {
                   echo "Cannot write to file ($filename)";
                   exit;
               }
            }//end while
           
           fclose($handle);
           
           //call the function to force the download the users computer
           force_download($filename);
                           
    }//end if
   
}//end if   
function force_download($file)
{
    //$dir="/path/to/file/";
    if (isset($file)) {
       header("Content-type: application/force-download");
       header('
Content-Disposition: inline; filename="' . $file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($file));
       header('
Content-Type: application/octet-stream');
       //header("Content-disposition: attachment; filename="".basename($file).""");
       header('
Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$file");
    } else {
       echo "No file selected";
    } //end if

}//end function

function show_form()
{
    echo "<html><body><h2> Table Data Exporter </h2>
    <h4>Creates a pipe delimited file of the table contents.</h4>
    <br><br><form name=myform action=".$_SERVER['
PHP_SELF']." method=post>
    Table Name:<input type=text name=mytable>
    <input type=submit name=submit value=Submit>
    </form></body></html>";

}       
?>




A simple script to count and report hits and the last modification time of an HTML page. Requires MySQL support (other DBs should work too, except possibly mSQL).
Categories : HTTP, MySQL, PHP, Databases
Identify and log search engine access (spiders, robots, etc.) to a page.
Categories : HTTP, Environment Variables, PHP, MySQL, Databases
Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
Download manager - A PHP script for adding a download page to any site.It also enables you track the no. of downloads.
Categories : PHP, Content Management, Filesystem, Databases, MySQL
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
Inserting data to a MySQL Database using AJAX
Categories : AJAX, HTTP, PHP, Databases, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
The simple counter with use MySql and gd.
Categories : MySQL, HTTP, Graphics, PHP, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
PHP Object Example of the Perl DBI with MySQL
Categories : PHP, PHP Classes, MySQL, Databases, Perl
How to thread a list of messages in database and show it in a treelike structure
Categories : PHP, MySQL, Databases
Add, Edit /Update & Delete all in one Contact Management Form
Categories : PHP, MySQL, Databases, Beginner Guides
Loading Images to/from MySQL
Categories : Databases, MySQL, PHP, Graphics
Checks Date-Input from HTML-Forms and converts to YYYY-MM-DD Format for MySQL Date-Fields
Categories : MySQL, Date Time, PHP, Databases
How to let a user download a picture by clicking on it instead of needing to right click and Save-As.
Categories : HTTP, PHP, HTML and PHP, Filesystem
 Ron Rutten wrote : 1156
typo-error at line 25: missing "); after database

$db2=mysql_select_db($dbname,$conn2) or die("Unable to connect to local database");
 
 matthew waygood wrote : 1158
You should avoid saving the data to a single file. What if 2 people execute the script just after each other? the first person may get the second persons information. You would be better to either use a random filename (perhaps using microtime) and delete the file afterwards or just keep the data in memory and output it directly to the browser.
 
 bastien koert wrote :1161
Hi Matt,

Your point is entirely valid. And in a normal operating environment I would do exactly that. However, the user that I worked on this for only needed the one file...

Like anything written here, its all adaptable and customizable to suit the users desired results.

Damn copy`n`paste...

Bastien