[MODIFIED] while () not working

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

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

[MODIFIED] while () not working

Post by $var »

what does it mean when this happens:

Code: Select all

$select = "SELECT * FROM surveys WHERE Mem_ID = '54'"	;            
$export = mysql_query($select) or die(mysql_error()); 
			echo $select;
			echo $export;
SELECT * FROM surveys WHERE Mem_ID = '54'
Resource id #2
the mysql_query($select) statement is wonky... why is it saying Resource id #2, i don't even know what that is.
Last edited by $var on Fri Mar 17, 2006 12:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_query() with selection type queries returns a resource to use for fetching the actual records found.

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

Post by $var »

okay, well, then that is not the problem... i have echoed out everything from this script, and it appears to not be grabbing the $row variable.
"while($row = mysql_fetch_row($export))"

Code: Select all

$select = "SELECT * FROM surveys WHERE Mem_ID = 54";
		$export = mysql_query($select) or die(mysql_error()); 
		$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";
			}
			}

this is the echo:
select: SELECT * FROM surveys WHERE Mem_ID = 54
export: Resource id #2:
fields: 31
header: Survey_ID Mem_ID Mem_Name Mem_FName Mem_LName Mem_Title Mem_URL Mem_Email Mem_City Mem_Country Mem_Postal Survey_1 Survey_2 Survey_3 Survey_3_2 Survey_4 Survey_4_2 Survey_5 Survey_5_2 Survey_6_1 Survey_6_2 Survey_6_3 Survey_6_4 Survey_6_5 Survey_6_6 Survey_6_7 Survey_6_8 Survey_6_9 Survey_6_10 Survey_7 Survey_8
row:
line:
value:
line:
data: (0) Records Found!
so it is appearant the drop off is the "while ($row...)"

any idea why that would be.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you are printing these after the while loop, You'll get trashed data.

Code: Select all

while($row = mysql_fetch_assoc($export))
{
  var_dump($row);
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I know it is a little more resource intensive, but you can read the result into an array, then run through the array to run the replacements you are doing...

Code: Select all

<?php
$sql = "SELECT * 
    FROM surveys 
    WHERE Mem_ID = 54";

$result = mysql_query($select) or die(mysql_error()); 
//$fields = mysql_num_fields($export);

while ($row = mysql_fetch_array($result))
{
    $my_array = $row;
}

$line = '';
foreach($my_array as $value) {                                            
    if ((!isset($value)) OR ($value == "")) {
        $value = "\t";
    } else {
        $value = str_replace('"', '""', $value);
        $value = '"' . $value . '"' . "\t";
    }    
    $line .= $value;
}
$data .= trim($line)."\n";
?>
Post Reply