Tuesday, 13 August 2013

update mysql table with php if statement

update mysql table with php if statement

I have a table with a int column named ssn and a column called male.
Now i want to update the male column with either 1 or 0 depending on the
second last digit being odd or even in the ssn column.
$sql = "SELECT * FROM db";
$result = $DBH->prepare($sql);
$result->execute();
foreach($result as $row) {
$male = $row['ssn'];
$male = substr($male, -2, 1);
if ( $male & 1 ) {
$gender = 1;//odd
}else
{
$gender = 0;};
$results= $DBH->prepare("UPDATE loandb_enkatsvardb SET male = $gender ")or
die(mysql_error());
$results->execute();
The error I recieve after loading for a long time is "Internal Server Error".
I also tried "insert ignore on duplicate" where it only saved "1's" in the
male column regardless of what the ssn column contained.
Cheers

No comments:

Post a Comment