[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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?
0
AUCKLANDIT
Asked:
AUCKLANDIT
  • 2
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now