INSERT and SELECT in single query MySQL

后端 未结 4 642
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 07:02

How can I do one single query with both an INSERT and SELECT in it and then read the results of selection? I want to insert and then select something, all must be d

4条回答
  •  無奈伤痛
    2020-12-20 07:19

    all must be done in one query...

    Why do you need to do everything in one query ?

    Like Wiseguy said, I think what you are looking for is called a transaction.

    Also, It might be a good idea considering updating to PDO, which will give you a more complete toolset like transactions and query parameters.

    Anyway, for answering your initial question, no it is not possible.

    Update: Here is an example of a transaction in PDO.

    try
    {
        $pdo->beginTransaction();
    
        $pdo->query(' ... ');
        $pdo->query(' ... ');
        $pdo->query(' ... ');
    
        $pdo->commit();
    }
    catch(Exception $e)
    {
        $pdo->rollback();
        die($e->getCode() . ': ' . $e->getMessage());
    }
    

提交回复
热议问题