PHP -> SQL result to a txt file

Hello,

I need to save a MYSQL result to a text file, I can kind of get it to work using $fd=fopen($filename,"w");
but I need input extra words and spacing etc into the text result into the text file.

The data in the table is: (CAPITALS is the row name, other is the data)

USERNAME: blabla
DOB: 05/05/2000
PHONE: 0303030
ADDRESS 1: 33 Tree Lane
ADDRESS 2: Bushland
STATE: CA


Ideally the saved result would look like this,

[blabla]
Age today: 10
Phone: 0303030
Address: 33 Tree Lane
                Bushland
                CA

Any idea's?
LVL 1
AUCKLANDITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb969Commented:
wow, this is a blast from the past, space management in a text file :)

first off, this only works when the character widths are constant, otherwise you'll have to look at something like a pdf creator.

Get some grid paper, and write out what you want the result to look like, with one character taking one cell on the grid paper.

1) When you are done, decide on the character position of the data to be printed.

2) Write a function that takes the field name as input and returns a string of spaces with a character length equivalent to the value rec'd in 1 above less the length of the field name

3) Write another function to print the field name, print the spaces returned in 2) above and also print the data.


For the additional address lines, you simply use the same process, but when sending the function 2) the field name, you simply send it a zero length string.

Hope that makes sense.
0
Ludwig DiehlSystems ArchitectCommented:
try this
<?php
$mysqli=new mysqli('host','user','pwd','db');
if(mysqli_connect_error())
    die(mysqli_connect_error());
   
$query="SELECT USERNAME,DOB,PHONE,ADDRESS_1,ADDRESS_2,STATE FROM TBL_CONTACT";
$rs=$mysqli->query($query);
if(!$rs)
    die($mysqli->error);
$filename='my_result.txt';
if(!file_exists($filename))
    $mode='wb+';
else
    $mode="ab+";    
$resource=fopen($filename,$mode);


if(!$resource)
    die('File could not be created/opened!');
$newline="\r\n";
while($result=$rs->fetch_assoc())
{
    
    $username   =$result['USERNAME'];
    $dob        =$result['DOB'];
    $phone      =$result['PHONE'];
    $address1   =$result['ADDRESS_1'];
    $address2   =$result['ADDRESS_2'];
    
    fwrite($resource,$newline); 
    fwrite($resource,"[$username]");
    fwrite($resource,$newline);    
    fwrite($resource,"DOB: $dob");
    fwrite($resource,$newline);    
    fwrite($resource,"PHONE: $phone");
    fwrite($resource,$newline);
    fwrite($resource,"ADDRESS_1: $address1");
    fwrite($resource,$newline);
    fwrite($resource,"ADDRESS_2: $address2");
    fwrite($resource,$newline);    
    
}
fclose($resource);
echo $rs->num_rows.' record(s) added';
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ludwig DiehlSystems ArchitectCommented:
You may want to try this new version I did which is generic and also more efficient
<?php
ini_set('display_errors',true);
error_reporting(E_ALL);
$mysqli=new mysqli('host','user','pwd','dbname');
if(mysqli_connect_error())
    die(mysqli_connect_error());
$query="SELECT USERNAME,DOB,PHONE,ADDRESS_1,ADDRESS_2,STATE FROM TBL_CONTACT";
$rs=$mysqli->query($query);
if(!$rs)
    die($mysqli->error);
$filename='my_result.txt';
if(!file_exists($filename))
    $mode='wb+';
else
    $mode="ab+";    
$resource=fopen($filename,$mode);
if(!$resource)
    die('File could not be created/opened!');
$newline    ="\r\n";
$texttowrite='';
$records    =$rs->num_rows;
while($result=$rs->fetch_assoc())
{
    $x=0;
    foreach($result as $key => $value)
    {
        if(!$x) //only first column of your query will be in brackets
        {
            $texttowrite.="[$value]";
            $x++;
        }
        else
            $texttowrite.="$key: $value";        
        $texttowrite.=$newline;
        $x++;
    } 
    $texttowrite.=$newline;   
}
if($texttowrite && $records)
    if(fwrite($resource,$texttowrite))
        echo $records.' record(s) added';
fclose($resource);
?>

Open in new window

0
AUCKLANDITAuthor Commented:
thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.