0

I try to lock the whole table.

Using the following codes: Issues: the if statement never print true, If I change to some other select statement with $sql, it works

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
$sql = "lock table tableA write";
if($stmt = mysqli_prepare($link, $sql))
    echo "true";

*Also for testing, there are no other codes in my php other these. Also I checked the user, has

GRANT SELECT, INSERT, UPDATE, LOCK TABLES ON *.* TO 'user'@'localhost'

I can not figure it out... and I am new to mysql with PHP as well.. Any help would be greatly appreciated

Edit: after added mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

it shows the error message:

PHP Fatal error: Uncaught mysqli_sql_exception: This command is not supported in the prepared statement protocol yet in /var/www/html/x.php:27 Stack trace: #0 /var/www/html/x.php(27): mysqli_prepare(Object(mysqli), 'lock table tableA...') #1 {main} thrown in /var/www/html/x.php on line 27

I found the solution.. you can call it directly with mysqli_query(..) since mysqli_prepare does not support locking.

if(mysqli_query($link,$sql)){
    echo "true";
}
  • What is the error message? Did you enable error reporting? [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Aug 07 '21 at 21:32
  • I have few other php files with mysqli, they works fine, but this is the first time with locking, So I do not want to update other files for now. – user16542536 Aug 07 '21 at 21:43
  • I updated the question, why stackoverflow considers this is a duplicated of a different questions... – user16542536 Aug 07 '21 at 21:50
  • 1
    Because the duplicate gave you the answer you needed. The message is now clear and correct, isn't it? https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html#prepared-statements-permitted You can't use `LOCK TABLE` in Prepared statements – Dharman Aug 07 '21 at 21:52
  • Thanks Dharman, the error message helps a lot. – user16542536 Aug 07 '21 at 22:08

0 Answers0