Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 done in one query...

I know it accept multiple commands in single query...But I want to read results of query and I cannot read results. I'm doing this:

$results=mysql_query("
INSERT INTO table1 (field1,field2) VALUES('aaa','bbbb') ON DUPLICATE KEY UPDATE `field1` = 'cccc', `field2`='dddddd'; 
SELECT field3 Form Table3 WHERE field3='eeeee';
",$connection);

while ($rows = mysql_fetch_array($results, MYSQL_NUM))  
echo $rows[0];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
490 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...