Page 1 of 1
[MODIFIED] while () not working
Posted: Fri Mar 17, 2006 10:53 am
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.
Posted: Fri Mar 17, 2006 10:55 am
by feyd
mysql_query() with selection type queries returns a resource to use for fetching the actual records found.
look at
mysql_fetch_array()
Posted: Fri Mar 17, 2006 11:33 am
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.
Posted: Fri Mar 17, 2006 4:01 pm
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);
}
Posted: Fri Mar 17, 2006 4:30 pm
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";
?>