Errors in File Handling program

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Errors in File Handling program

Post by phphunger »

Hi,

i have created 2 files namely db.php and functions.php when i run the db.php many errors coming like
Connection Successfull

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45

Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 45
Data appended to the file
Status changed from 0->1
Warning: fclose(): supplied argument is not a valid stream resource in C:\xampp\htdocs\Db2File\functions.php on line 65

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\Db2File\functions.php on line 66

Code:(db.php)

Code: Select all

<?php
 
            include("functions.php");
            //include("functions.php");
            dbConnection();
 
//Main logic Code
            
            fetchRecord();
 
            processData();
       
?>
Code:(functions.php)

Code: Select all

<?php
function dbConnection(){
//connection parameters
            $username = "root";
            $password = "";
            $dbhost = "localhost";
 
//connection to database
            $dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database". mysql_error());
 
//selecting a database
            $dbselect = mysql_select_db("test", $dbconnection);
 
//checking whether the database is selected
            if(!$dbselect){
                echo "Error in Connection String";
            }
            else
            {
                echo "Connection Successfull";
            }
            echo "<br/>";
}
 
function fetchRecord(){
    $query="SELECT * FROM phone_dummy";
            $result=mysql_query($query);
            //$num=mysql_numrows($result);
 
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
            while ($row = mysql_fetch_assoc($result)) {
                $phone_id = $row['phone_id'];
                $phone_number = $row['phone_number'];
                $name = $row['name'];
                $address = $row['address'];
                $status = $row['status'];
 
                $export="$phone_id|$phone_number|$name|$address|$status"."\r\n";
 
                $stringData = "$export";
                fwrite($fh, $stringData);
 
                if($status == 0){
                    $updatequery = "UPDATE phone_dummy SET status = 1";
                    mysql_query($updatequery);
                  }
            }
}
 
function fileOpen(){
            $header="ID|Phone|Name|Address|Status";
            $myFile = "phonedetails.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");            
}
function fileWrite(){
            $stringData = "$header"."\r\n";
            fwrite($fh, $stringData);
}
 
function fileClosing(){
            fclose($fh);
            mysql_free_result($result);
}
 
function processData(){
    fileOpen();
    
            echo "Data appended to the file"."<br>";
            echo "Status changed from 0->1";
    fileClosing();
}
 
?>
 
 
Can anyone provide solution.
Last edited by phphunger on Mon Aug 24, 2009 6:03 am, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

You're opening the file after you're attempting to write to it. Needs to be the other way round ;)
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

Can you tell me where exactly i am opening...i am little bit confused..
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

You are using $fh on line 45 (your error) to write to the file. But you have not set $fh, which is supposed to be a file stream resource.

However, you are setting it on line 57...but that's a bit late :P

You need to supply fwrite() with an fopen() file handle as its first argument. Instead, you're supplying it with an empty variable.
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

Connection Successfull
Data appended to the file
Status changed from 0->1
Fatal error: Call to undefined function fileclosing() in C:\xampp\htdocs\Db2File\functions.php on line 77

Code: Select all

<?php
 
function dbConnection(){
//connection parameters
            $username = "root";
            $password = "";
            $dbhost = "localhost";
 
//connection to database
            $dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database". mysql_error());
 
//selecting a database
            $dbselect = mysql_select_db("test", $dbconnection);
 
//checking whether the database is selected
            if(!$dbselect){
                echo "Error in Connection String";
            }
            else
            {
                echo "Connection Successfull";
            }
            echo "<br/>";
}
 
function fetchRecord(){
    $query="SELECT * FROM phone_dummy";
            $result=mysql_query($query);
            //$num=mysql_numrows($result);
 
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
            while ($row = mysql_fetch_assoc($result)) {
                $phone_id = $row['phone_id'];
                $phone_number = $row['phone_number'];
                $name = $row['name'];
                $address = $row['address'];
                $status = $row['status'];
 
                $export="$phone_id|$phone_number|$name|$address|$status"."\r\n";
 
                $stringData = "$export";
                $myFile = "phonedetails.txt";
                $fh = fopen($myFile, 'w') or die("Can't open file");
                fwrite($fh, $stringData);
                if($status == 0)
                {
                    $updatequery = "UPDATE phone_dummy SET status = 1";
                    mysql_query($updatequery);
                }
            }
}
 
function fileOpen(){
            $header="ID|Phone|Name|Address|Status";
            $myFile = "phonedetails.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
}
 
function fileWrite(){
            $stringData = "$header"."\r\n";
            fwrite($fh, $stringData);
}
 
function fileClosing(){
            fclose($fh);
            
}
 
function processData(){
           fileOpen();
           echo "Data appended to the file"."<br>";
           echo "Status changed from 0->1";
           fileClosing();
}
 
/*
//File will be rewritten if already exists
   function writetofile($filename,$newdata) {
          $filename = "phonedetails.txt";
          $f=fopen($filename,"w");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function append_file($filename,$newdata) {
          $f=fopen($filename,"a");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function read_file($filename) {
          $f=fopen($filename,"r");
          $data=fread($f,filesize($filename));
          fclose($f);
          return $data;
   }
 
$records = processData($limit);
foreach($records as $record){
    $status = writetofile($record);
    updateStatus($status);
}
 
function processData($limit){
    $result = array();
    $selectsql = "SELECT * FROM phone_dummy where status = 0 limit 0, $limit";
    $resultset = mysql_query($selectsql);
    while($resultrow = mysql_fetch_array($resultset)){
            $result = $resultrow;
    }
    return $result;
}
 
function updateStatus($status){
    $updatesql = "UPDATE phone_dummy SET status = 1";
    mysql_query($updatesql);
}
*/
 
/*
function writetofile($path, $record, $mode="w+")
{
    $path = "../phonedetails.txt";
    if (file_exists($path) && !is_writeable($path))
    {
    return false;
    }
    if ($fp = fopen($path, $mode))
    {
        fwrite($fp, $record);
        fclose($fp);
    }
    else
    {
        return false;
    }
    return true;
}
*/
 
?>
Last edited by phphunger on Mon Aug 24, 2009 6:39 am, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

Read the error - what do you think it means?

Where are you setting $myFile?
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

thanks mate for the reply,..... again i got new task to the current code........ will get back to you
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

Cool :)

You got it working?
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

the task..........

i have to fetch the data from the database and store it in a result and pass that result to the function process data where the result is written to the files by passing the arguments which is the result

db.php

Code: Select all

<?php
            include("functions.php");
            //include("functions.php");
            dbConnection();
//Main logic Code
             fetchRecord();
             while ($row = mysql_fetch_array($result))
                {
                    //$phone_id = $row['phone_id'];
                    //$phone_number = $row['phone_number'];
                    //$name = $row['name'];
                    //$address = $row['address'];
                    //$status = $row['status'];
 
                    //$newres = "$phone_id|$phone_number|$name|$address|$status";
                    $newres = $row;
                }
                return $newres;
             processData($newres);
             
 
?>
functions.php

Code: Select all

<?php
 
function dbConnection(){
//connection parameters
            $username = "root";
            $password = "";
            $dbhost = "localhost";
 
//connection to database
            $dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database". mysql_error());
 
//selecting a database
            $dbselect = mysql_select_db("test", $dbconnection);
 
//checking whether the database is selected
            if(!$dbselect){
                echo "Error in Connection String";
            }
            else
            {
                echo "Connection Successfull";
            }
            echo "<br/>";
}
 
function fetchRecord(){
    $query="SELECT * FROM phone_dummy";
            $result=mysql_query($query);
            //$num=mysql_numrows($result);
 
            if (!$result)
            {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
            return($result);
 
                //$export="$phone_id|$phone_number|$name|$address|$status"."\r\n";
 
                //$stringData = "$export";
                //$myFile = "phonedetails.txt";
                //$fh = fopen($myFile, 'w') or die("Can't open file");
                //fwrite($fh, $stringData);
                if($status == 0)
                {
                    $updatequery = "UPDATE phone_dummy SET status = 1";
                    mysql_query($updatequery);
                }
            
}
 
function fileOpen()
{
            $header="ID|Phone|Name|Address|Status";
            $myFile = "phonedetails.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
}
 
function fileWrite()
{
            $stringData = "$newres"."\r\n";
            fwrite($fh, $stringData);
}
 
function fileClosing()
{
            fclose($fh);
            
}
 
function processData()
{
                
           fileOpen();
           fileWrite($newres);
           echo "Data appended to the file"."<br>";
           echo "Status changed from 0->1";
           fileClosing();
}
 
/*
//File will be rewritten if already exists
   function writetofile($filename,$newdata) {
          $filename = "phonedetails.txt";
          $f=fopen($filename,"w");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function append_file($filename,$newdata) {
          $f=fopen($filename,"a");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function read_file($filename) {
          $f=fopen($filename,"r");
          $data=fread($f,filesize($filename));
          fclose($f);
          return $data;
   }
 
$records = processData($limit);
foreach($records as $record){
    $status = writetofile($record);
    updateStatus($status);
}
 
function processData($limit){
    $result = array();
    $selectsql = "SELECT * FROM phone_dummy where status = 0 limit 0, $limit";
    $resultset = mysql_query($selectsql);
    while($resultrow = mysql_fetch_array($resultset)){
            $result = $resultrow;
    }
    return $result;
}
 
function updateStatus($status){
    $updatesql = "UPDATE phone_dummy SET status = 1";
    mysql_query($updatesql);
}
*/
 
/*
function writetofile($path, $record, $mode="w+")
{
    $path = "../phonedetails.txt";
    if (file_exists($path) && !is_writeable($path))
    {
    return false;
    }
    if ($fp = fopen($path, $mode))
    {
        fwrite($fp, $record);
        fclose($fp);
    }
    else
    {
        return false;
    }
    return true;
}
*/
 
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

What's the problem?
MarshmaLLows08
Forum Newbie
Posts: 2
Joined: Mon Aug 24, 2009 7:51 am
Location: Philippines
Contact:

Re: Errors in File Handling program

Post by MarshmaLLows08 »

i encounter some error when i post my php in free webhost... what will i do.... can i get your code to prevent my error?
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

i have done upto this but im getting only one record, i guess do i have to do the loop...... kindly help me


db.php

Code: Select all

<?php
            include("functions.php");
            //include("functions.php");
            dbConnection();
//Main logic Code
             $result = fetchRecord();
             
 
             processData($result);
             
 
?>
functions.php

Code: Select all

<?php
 
function dbConnection(){
//connection parameters
            $username = "root";
            $password = "";
            $dbhost = "localhost";
 
//connection to database
            $dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database". mysql_error());
 
//selecting a database
            $dbselect = mysql_select_db("test", $dbconnection);
 
//checking whether the database is selected
            if(!$dbselect){
                echo "Error in Connection String";
            }
            else
            {
                echo "Connection Successfull";
            }
            echo "<br/>";
}
 
function fetchRecord(){
    $query="SELECT * FROM phone_dummy";
            $result=mysql_query($query);
            //$num=mysql_numrows($result);
 
            if (!$result)
            {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
            return($result);
 
                //$export="$phone_id|$phone_number|$name|$address|$status"."\r\n";
 
                //$stringData = "$export";
                //$myFile = "phonedetails.txt";
                //$fh = fopen($myFile, 'w') or die("Can't open file");
                //fwrite($fh, $stringData);
                //if($status == 0)
                //{
                    //$updatequery = "UPDATE phone_dummy SET status = 1";
                    //mysql_query($updatequery);
                //}
            
}
 
function fileOpen()
{
            $header="ID|Phone|Name|Address|Status";
            $myFile = "phonedetails.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
}
 
function fileWrite($export)
{
            $myFile = "phonedetails.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData = "$export"."\r\n";
            fwrite($fh, $stringData);
            fclose($fh);
 
}
 
 
 
function processData($result)
{
                
           fileOpen();
           while ($row = mysql_fetch_assoc($result)) {
                $phone_id = $row['phone_id'];
                $phone_number = $row['phone_number'];
                $name = $row['name'];
                $address = $row['address'];
                $status = $row['status'];
 
                $export="$phone_id|$phone_number|$name|$address|$status";
 
           fileWrite($export);
           }
           if($status == 0){
                    $updatequery = "UPDATE phone_dummy SET status = 1";
                    mysql_query($updatequery);
                  }
           
           echo "Data appended to the file"."<br>";
           echo "Status changed from 0->1";
           
 
}
 
/*
//File will be rewritten if already exists
   function writetofile($filename,$newdata) {
          $filename = "phonedetails.txt";
          $f=fopen($filename,"w");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function append_file($filename,$newdata) {
          $f=fopen($filename,"a");
          fwrite($f,$newdata);
          fclose($f);
   }
 
   function read_file($filename) {
          $f=fopen($filename,"r");
          $data=fread($f,filesize($filename));
          fclose($f);
          return $data;
   }
 
$records = processData($limit);
foreach($records as $record){
    $status = writetofile($record);
    updateStatus($status);
}
 
function processData($limit){
    $result = array();
    $selectsql = "SELECT * FROM phone_dummy where status = 0 limit 0, $limit";
    $resultset = mysql_query($selectsql);
    while($resultrow = mysql_fetch_array($resultset)){
            $result = $resultrow;
    }
    return $result;
}
 
function updateStatus($status){
    $updatesql = "UPDATE phone_dummy SET status = 1";
    mysql_query($updatesql);
}
*/
 
/*
function writetofile($path, $record, $mode="w+")
{
    $path = "../phonedetails.txt";
    if (file_exists($path) && !is_writeable($path))
    {
    return false;
    }
    if ($fp = fopen($path, $mode))
    {
        fwrite($fp, $record);
        fclose($fp);
    }
    else
    {
        return false;
    }
    return true;
}
*/
 
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Errors in File Handling program

Post by jackpf »

You'll need to put processData() in the loop I believe, otherwise it's just waiting until the end of the loop, and getting the last variables set.

But I don't know - this is your code!! You should know how it works.
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

yeah u r right, actually im getting the last record only, the loop is terminating after writing one file
phphunger
Forum Commoner
Posts: 45
Joined: Tue Aug 11, 2009 11:56 pm

Re: Errors in File Handling program

Post by phphunger »

jackpf can you help me where should i keep the loop.............
Post Reply