简体   繁体   中英

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!

Check this manual http://php.net/manual/en/pdo.prepare.php and it should be clear,,

but to create a new Persoon Object inside a while loop without using unset($persoon) after is bad memory management.. And creating a object inside a loop isn't very good better should be to reuse the same created object again and again and again something like below

$person = new Person();

while ( $row = $st->fetch() ) {

     person->populateFromArray($row)

replace this:

WHEN lastCO = null 

with:

WHEN lastCO is null 

comparison between variable and NULL value must be done with IS NULL statement, otherwise is always UNKNOWN so your THEN branch has been not executed.

Another thing not right for me, in your query.

You use CASE but if lastCO is not null and is greater than of your variabile, the value of lastCO set to NULL so you may put ELSE branch to prevent this behaviour.

At the end, you can merge to query in only one UPDATE query without loop.

Edit about answer of comment:

Try this: In this query you update all records of persons table because you haven't put a where clause

UPDATE persons
SET lastCO = 
CASE 
    WHEN lastCO < '$person->encPastDate'
    THEN '$person->encPastDate'                      
    WHEN lastCO is null
    THEN '$person->encPastDate' 
    ELSE lastCO = lastCO
END

In other way: In this way you update only record of persons where lastCO is not valorized or if lower than variable '$person->encPastDate'

UPDATE persons
SET lastCO = 
CASE 
    WHEN lastCO < '$person->encPastDate'
    THEN '$person->encPastDate'                      
    WHEN lastCO is null
    THEN '$person->encPastDate' 
END
where lastCO is null or lastCO < '$person->encPastDate'

Third way: You have a loop, every step you process a person. So you can limit your update at that person using in where clause: persons.id = $person->id or other variable where has stored person ID.

Tell me if it's OK

$conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);

Cant do something while an unbuffered query was running in the WHILE loop. Add the following line to ensure buffered queries are being used.

EDIT

Of course it does this you need a where condition in the sql.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM