[英]Create Table In PDO Transaction
How can I fix this transaction so that the pdo query makes a new table in step #4?我如何修复此事务以便 pdo 查询在步骤 #4 中创建一个新表?
The first three steps work, but I can't seem to get #4 to work.前三个步骤有效,但我似乎无法让#4 工作。
STEPS脚步
4. Create a table with the id of both users as the title like this 2+13 (2 being the id and 13 being the id) 4.创建一个表,标题是两个用户的id,像这样2+13(2是id,13是id)
$userid = "123456";
$firstname = "Dae";
$oglang = "engs";
$status = 0;
$pdo->beginTransaction();
try{
// Find a user with a status of 0
$sql = "SELECT id FROM users WHERE chattingstatus = :status";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':status' => $status)
);
$freeuser = $stmt->fetchColumn();
//put the original user into the database with userid firstname and language
$sql = "INSERT INTO users (userid, firstname, oglang, chattingstatus) VALUES (:userid, :firstname, :oglang, :chattingstatus)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':userid' => $userid, ':firstname' => $firstname, ':oglang' => $oglang, ':chattingstatus' => 0)
);
$ogID = $pdo->lastInsertId();
// change the chattingstatus of 0 of the free user to 1
$sql = "UPDATE users SET chattingstatus = 1 WHERE id = :freeuser";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':freeuser' => $freeuser)
);
//query 3 CHANGE STATUS OF ORIGINAL USER from 0 to 1
$sql = "UPDATE users SET chattingstatus = 1 WHERE userid = :oguser";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':oguser' => $userid)
);
//query 4: Make a table between the 2 users with their IDs
$table = $freeuser."+".$ogID;
$sql ="CREATE table $table(
ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
Messages VARCHAR( 50 ) NOT NULL);";
$stmt = $pdo->exec($sql);
print("Created $table Table.\n");
$pdo->commit();
}
//Our catch block
catch(Exception $e){
//Print out the error message.
echo $e->getMessage();
//Rollback the transaction.
$pdo->rollBack();
}
Thanks in advance.提前致谢。
Since your table name includes the special character +
, you need to put it in backticks to quote it.由于您的表名包含特殊字符
+
,因此您需要将其放在反引号中以引用它。
$sql ="CREATE table `$table` (
ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
Messages VARCHAR( 50 ) NOT NULL);";
You'll need to remember to put backticks around the table name whenever you use it in other queries.每当您在其他查询中使用表名时,您都需要记住在表名周围加上反引号。 If you insist on having per-user tables like this, you might want to use a different character to connect them, like underscore.
如果您坚持使用这样的每个用户表,您可能希望使用不同的字符来连接它们,例如下划线。
Creating table in transation doesn't work in MySQL:在事务中创建表在 MySQL 中不起作用:
Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction.
当在事务中发出数据库定义语言 (DDL) 语句(例如 DROP TABLE 或 CREATE TABLE)时,某些数据库(包括 MySQL)会自动发出隐式 COMMIT。 The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary.
隐式 COMMIT 将阻止您回滚事务边界内的任何其他更改。 Source: https://www.php.net/manual/en/pdo.begintransaction.php
资料来源: https ://www.php.net/manual/en/pdo.begintransaction.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.