Zedkay22
Zedkay22

Reputation: 454

Create Table In PDO Transaction

How can I fix this transaction so that the pdo query makes a new table in step #4?

The first three steps work, but I can't seem to get #4 to work.

STEPS

  1. Finds a user with a chattingstatus of 0 in the database
  2. Add a user into the database (with predetermined variables)
  3. change the chattingstatus from 0 to 1 for both the user with a 0 status and the inserted user

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)

$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.

Upvotes: 0

Views: 596

Answers (2)

long
long

Reputation: 4310

Creating table in transation doesn't work in 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. The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary. Source: https://www.php.net/manual/en/pdo.begintransaction.php

Upvotes: 1

Barmar
Barmar

Reputation: 780655

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.

Upvotes: 1

Related Questions