Page 1 of 2

[UNSOLVED] fopen() YARGH!

Posted: Tue Feb 21, 2006 10:22 am
by $var
A few months back I asked about this code, and eventually got it to work... and while i moved on in projects, someone pointed out that it has ceased working...
I don't feel that I have made any changes to the page, or the database but it now exports the error (0) Records Found!
I echoed out the select, and it is pulling the right ID still, so i am not quite sure where the problem is... the actual data is in the database.

Code: Select all

<? $select = "SELECT * FROM surveys WHERE Mem_ID =".$ID;                
		$file = "surveys.txt";
		$export = mysql_query($select);
		$fields = mysql_num_fields($export);
			for ($i = 0; $i < $fields; $i++) {
		    $header .= mysql_field_name($export, $i) . "\t";
			while($row = mysql_fetch_row($export)) {
				$line = '';
				foreach($row as $value) {                                            
					if ((!isset($value)) OR ($value == "")) {
						$value = "\t";
					} else {
						$value = str_replace('"', '""', $value);
						$value = '"' . $value . '"' . "\t";
					}
					$line .= $value;
				}
				$data .= trim($line)."\n";
			}
			}
			$data = str_replace("\r","",$data); 
			
			if ($data == "") {
		    $data = "\n(0) Records Found!\n";                        
			} 
			if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }  
			if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }  
			header("Location: http://www.advantageboard.com/access/02 ... elcome.php");  
			fclose($file_handle);  
?>

Posted: Tue Feb 21, 2006 10:25 am
by shiznatix
change line 3 to this

Code: Select all

$export = mysql_query($select) or die(mysql_error());

Posted: Tue Feb 21, 2006 10:37 am
by s.dot
If the above does not produce an error, see if it is actually fetching any results.
If it isn't, i'd think it was a database change.

Code: Select all

$export = mysql_query($select);
echo mysql_num_rows($export);
That, and you could echo out your data during and after the loop to see if it gets lost during processing.

Posted: Tue Feb 21, 2006 10:38 am
by $var
well, i changed that and it is working...
if you don't mind... but out of nowhere, why would that effect it?

like, it worked about 20 times over the course of a few monthes and then... nothing.

also, it no longer works as tab delimited in an excell document,
it does tab (/t) the export properly, but why copied and pasted into XLS... it shows up in one field.

Posted: Tue Feb 21, 2006 10:47 am
by s.dot
hmm, odd.
the two above posts were debugging suggestions and should'nt have changed the execution of your script :P

Posted: Tue Feb 21, 2006 10:48 am
by feyd
if Excel doesn't know it's a tab delimited file it can easily get confused and not load it right.

Posted: Tue Feb 21, 2006 11:06 am
by $var
so, there is a way to set it tab delimited... i also know that there is a way to have it set comma delimited, but that seems like a variable someone would put into an answer, and might not be a concrete solution anyways.

Posted: Tue Feb 21, 2006 11:57 am
by $var
for the record...
the tab delimited read properly when the survey.txt was imported through the excell program, and not simply pasted in.

Posted: Tue Feb 21, 2006 1:09 pm
by $var
i'm sorry for making so many posts in a row, and i know that that is against the policy of the board, but i found something that is fishy about the function, and it is related to this, so i hope i don't get the thread locked (i'm sorry feyd)

okay, so i am STILL getting the (0) records found when i log on as different users. the deal is this:

- log on with a fresh session
- complete the survey
- survey says (0) records found
- complete the survey again, using the same session
- survey exports properly

there is something about the initial completion of the survey that is glitching, and then when resent, it's fine.
thing is, no one else would resubmit, since they didn't know it was wrong...
as well, this is strange, no?

Posted: Tue Feb 21, 2006 1:22 pm
by feyd
constructive posts are fine.

I would guess your $ID is invalid for the first pass.

Posted: Tue Feb 21, 2006 1:28 pm
by $var
SELECT * FROM surveys WHERE Mem_ID =69
so it's going through, it echos fine as well...
but again, i checked the export, and it's (0) Records Found!


what would make the ID invalid?

Posted: Tue Feb 21, 2006 1:31 pm
by feyd
considering I have no idea how $ID is set, I can't say.

Posted: Tue Feb 21, 2006 1:34 pm
by nickman013
Add this to the VERY top of the page

Code: Select all

error_reporting('E_ALL");

Posted: Tue Feb 21, 2006 1:34 pm
by $var
ah... i see... the ID is the cookie that allows the user to access the site.
it is the _db ID associated with their account.

the cookie must be there, or they wouldn't be allowed to access that section
and again, it echos out and enters into the database as a valid ID

Posted: Tue Feb 21, 2006 1:37 pm
by nickman013
Glad you fixed it.

:D