Trouble Using SQL SELECT and then UPDATE in One PHP Function - MYSQL PHP
ST EXECUTE PDO
UPDATE: Solved, thanks for the help everyone!
So I want to have a PHP function which queries the database for all
encounters of type 'Check-out' and then updates the persons table field
lastCO with the date of whichever of the encounters is most recent.
But in my while loop for my UPDATE function seems to error out somehow.
You can tell I put in an 'echo' statement just so I could see if the
conditions of the loops were even being met, and indeed when I looked at
the source it echo'd one time.
But when I comment out my second (UPDATE) SQL statement, the while loop
seems to iterate properly (and my echo statement prints many, many times).
public static function getLastCO() {
$conn = new PDO( DB, DBU, DBP );
$sql = "SELECT UNIX_TIMESTAMP(encPastDate) AS encPastDate,
encPastText,encPastPTRef
FROM enctsPast WHERE encPastText = 'Check-out'";
$st = $conn->prepare( $sql );
$st->execute();
$row = $st->fetch();
//trouble seems to start here!!!!
while ( $row = $st->fetch() ) {
$person = new Person( $row );
if ($person->encPastDate != null){
echo '123abc456';
$sql1 = "UPDATE persons SET lastCO =
(CASE WHEN lastCO < '$person->encPastDate' THEN
'$person->encPastDate'
WHEN lastCO = null THEN '$person->encPastDate' END)";
$st = $conn->prepare( $sql1 );
$st->execute();
}
}
$conn = null;
}
So what is wrong with my UPDATE statement and execution? Is the problem
the way I connect to two different tables while on the same 'connection'?
Something else?
I am just having trouble debugging this, and my pitiful echo attempt isn't
getting me far : /
-
Thanks in advance for your help!
No comments:
Post a Comment