简体   繁体   English

MySQL更新并在一条语句中选择

[英]MySQL Update and Select in one statement

I am attempting to do an UPDATE and a SELECT in the same sql statement. 我试图在同一条sql语句中执行UPDATE和SELECT。 For some reason, the below code is failing. 由于某种原因,以下代码失败。

$sql = "UPDATE mytable SET last_activity=CURRENT_TIMESTAMP, 
info1=:info1, info2=:info2 WHERE id = {$id};";

$sql .= "SELECT id, info1, info2 FROM myTable 
WHERE info1 >=:valueA AND info2>:valueB;"

$stmt = $conn->prepare($sql);
$stmt->bindParam(":info1", $info1);
$stmt->bindParam(":info2", $info2);

$stmt->bindParam(":valueA", $valueA);
$stmt->bindParam(":valueB", $valueB);

$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($result);

QUESTION: what might I be doing wrong? 问题:我可能做错了什么? I have been spending hours on this issue knowing that it's probably a small error right under my nose. 我花了几个小时在这个问题上,知道这可能是我的鼻子下的一个小错误。


Edited: 编辑:

I obtained this error message when loading the page that contains the php code: 加载包含php代码的页面时,我获得了此错误消息:

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in ajaxCall.php:89 Stack trace: #0 ajaxCall.php(89): PDOStatement->fetchAll(2) #1 {main} thrown in ajaxCall.php on line 89 ajaxCall.php:89中消息“ SQLSTATE [HY000]:常规错误”的未捕获异常“ PDOException”,堆栈跟踪:#0 ajaxCall.php(89):PDOStatement-> fetchAll(2)#1 {main}抛出了ajaxCall。 PHP的第89行

I am using ajax to call the php page that contains the above code, and when I load the php page from the browser, I get the above error message. 我正在使用ajax来调用包含上述代码的php页面,并且当我从浏览器加载php页面时,出现了以上错误消息。

Line 89 is: $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 第89行是: $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

Since you are running two queries, you need to call nextRowset to access the results from the second one. 由于您正在运行两个查询,因此需要调用nextRowset来访问第二个查询的结果。

So, do it like this: 所以,这样做:

// code
$stmt->execute();
$stmt->nextRowset();
// code

When you run two or more queries, you get a multi-rowset result. 当您运行两个或多个查询时,您将获得一个多行结果。 That means that you get something like this (representation only, not really this) : 这意味着您将获得以下内容(仅表示形式, 并非如此)

Array(
    [0] => rowset1,
    [1] => rowset2,
    ...
)

Since you want the second set -the result from the SELECT -, you can consume the first one by calling nextRowset . 由于要第二个集合(即SELECT的结果),因此可以通过调用nextRowset 使用第一个nextRowset That way, you'll be able to fetch the results from the 'important' set. 这样,您将能够从“重要”集中获取结果。
(Even though 'consume' might not be the right word for this, it fits for understanding purposes) (即使“消费”可能不是正确的词,它也适合理解)

Executing two queries with one call is only allowed when you are using mysqlnd . 仅当使用mysqlnd时,才允许一次调用执行两个查询。 Even then, you must have PDO::ATTR_EMULATE_PREPARES set to 1 when using prepared statements. 即使这样,在使用准备好的语句时,也必须将PDO::ATTR_EMULATE_PREPARES设置为1 You can set this using: 您可以使用以下方法进行设置:

$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);

Alternatively, you can use $conn->exec($sql) , which works regardless. 另外,您可以使用$conn->exec($sql) ,无论如何都可以。 However, it will not allow you to bind any data to the executed SQL. 但是,它不允许您将任何数据绑定到已执行的SQL。

All in all, don't execute multiple queries with one call. 总而言之,不要一次调用执行多个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM