Advertisement
cdsatrian

write to file

Jun 19th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2. $file = "c:\tes.txt";
  3. if(!(file_exists($file)))
  4. {
  5.   $handle = fopen($filecontoh , 'w') or die('Gagal membuka: '.$filecontoh );
  6.   $dbhost='localhost';
  7.   $dbuser='root';
  8.   $dbpass='';
  9.   $dbname='test';
  10.   $con = mysql_connect($dbhost,$dbuser,$dbpass);
  11.   $sb= mysql_select_db($dbname,$con);
  12.   $query = 'SELECT * FROM detail';
  13.   if($hasil = mysql_query($query))
  14.   {
  15.     if(mysql_num_rows($hasil)>0)
  16.     {
  17.       $content='';
  18.       while($data=mysql_fetch_array($hasil))
  19.       {
  20.         $content.=str_pad($data['id'],2,STR_PAD_LEFT)
  21.                 .str_pad($data['nama'],25,STR_PAD_LEFT)
  22.                 .str_pad($data['alamat'],25,STR_PAD_LEFT)
  23.                 .str_pad($data['telp'],12,STR_PAD_LEFT)."\n";
  24.       }
  25.       fwrite($handle, $content);
  26.       fclose($file_anyar);
  27.       echo "File berhasil dibuat";
  28.     }
  29.     else
  30.     {
  31.       echo "tidak ditemukan data di tabel";
  32.     }
  33.   }
  34.   else
  35.   {
  36.     echo "query data tidak berhasil";
  37.   }
  38. }
  39. else
  40. {
  41.   echo "File tidak jadi dibuat karena file $file sudah ada";
  42. }
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement