[UNSOLVED] fopen() YARGH!

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

User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

[UNSOLVED] fopen() YARGH!

Post 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);  
?>
Last edited by $var on Tue Feb 21, 2006 1:05 pm, edited 3 times in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

change line 3 to this

Code: Select all

$export = mysql_query($select) or die(mysql_error());
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hmm, odd.
the two above posts were debugging suggestions and should'nt have changed the execution of your script :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if Excel doesn't know it's a tab delimited file it can easily get confused and not load it right.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

constructive posts are fine.

I would guess your $ID is invalid for the first pass.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

considering I have no idea how $ID is set, I can't say.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Add this to the VERY top of the page

Code: Select all

error_reporting('E_ALL");
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Glad you fixed it.

:D
Post Reply