Export to CSV and update exported status in MySQL

Hi

I want to export records to CSV while updating each records Export status to ‘Y’

The import and update cannot run together.

The update works but I end up with a blank file.
If i comment out the update statement i end up with a populated csv file.

See sample code below

[php]
#set hearder record for csv file
$data .= “OrderNumber,Price, \r”;

#prepare sql statement to get the data
$sql = “select id, order_number, price from orders where exported=‘N’;”;

#get the data
$rs = mysql_query($sql,$conn);

##Loop through each record and store values from DB in variable.
##concatenate each record to $data variable.
###Furthermore prepare the update sql statement to set the exported status to ‘Y’.
###concatenate each update statement to $sql_upate variable.
for ($i=0; $i < mysql_numrows($rs); $i++){
$id= @mysql_result($rs,$i,“id”);
$order_number = @mysql_result($rs, $i,“order_number”);
$price= @mysql_result($rs, $i,“price”);

$data .= "$order_number, $price, \r";	

$sql_update .= "update orders set exported='Y' where id=$id and exported='N';";		

}

#update exported status per record
mysql_query($sql_update, $conn) ;

header(‘Cache-Control: public’);
header(‘Content-type: application/csv’);
header(“Content-Disposition: attachment; filename=“data.csv””);

#print each record in CSV file
echo $data;
[/php]

you use :
mysql_numrows($rs)

And the correct syntax are :
mysql_num_rows($rs)

Doesn’t seem to make a difference. mysql_numrows works fine.

Sponsor our Newsletter | Privacy Policy | Terms of Service