简体   繁体   中英

PHP PDO Not Locking MySQL Table(s)

I'm trying to run this command in my PHP script to lock my table:

$PDO->exec('LOCK TABLES `tblclients` WRITE');

Once I run that script, I attempt to SELECT from that table inside MySQL Workbench just to verify the table is actually locked. The SELECT runs smoothly, which tells me the table has never actually been locked. Am I right?

If I just run the LOCK TABLES tblclients WRITE from inside MySQL Workbench, then attempt to SELECT the table does show as LOCKED and prevents the SELECT from firing off.

It just doesn't seem to be working when I run that query from my PHP script.

In both cases I'm running locally as the root user so I have all privileges.

Any ideas?

MySQL LOCK TABLES is a per-session operation. If you run something like

<?php
$PDO->exec("LOCK TABLES tblclients WRITE");
exit;
?>

then the mysql connection from PDO is closed immediately and the lock will be released. In order to properly test this you may try using two separate connections and select with the one that does not lock or sleep for a period of time so you can run the query manually.

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