首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PDO事务中创建表

在PDO事务中创建表
EN

Stack Overflow用户
提问于 2017-07-13 21:42:01
回答 1查看 383关注 0票数 0

如何修复此事务,以便pdo查询在步骤4中生成一个新表?

前三个步骤起作用,但我似乎不能让#4开始工作。

步骤

  1. 在数据库中查找聊天状态为0的用户。
  2. 向数据库中添加一个用户(带有预定变量)
  3. 将具有0状态的用户和插入的用户的聊天状态从0更改为1

4.创建一个以两个用户的id作为标题的表,如这个2+13 (2是id,13是id)

代码语言:javascript
运行
复制
$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();
}

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-02-07 15:19:16

在MySQL中,在传输中创建表不起作用:

当事务中发出数据库定义语言(DDL)语句(如database或CREATE )时,某些数据库(包括MySQL )会自动发出隐式提交。隐式提交将阻止您回滚事务边界内的任何其他更改。来源:https://www.php.net/manual/en/pdo.begintransaction.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45091378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档