【发布时间】:2015-07-20 14:52:31
【问题描述】:
在我的 SQL 查询中,我有一个 INSERT、UPDATE 和一个 SELECT 查询全部在一个查询中,如下面的查询
INSERT INTO questions (user_mail, question_id, question, username, created_at) VALUES (:fid, :qid, :com, :pid, :dat);
UPDATE users SET activity = activity + 1 WHERE face_id = :fid;
SELECT questions.face_id
FROM questions
WHERE questions.question_id = :qid
我在一个 PDO 查询中拥有所有这三个查询,但是有一个奇怪的规则使用 UPDATE 查询和 $result = $stmt->fetchAll(); 任何关于如何解决这个问题的想法,因为我的 PDO 不断吐出一个错误,上面写着
SQLSTATE[HY000]:一般错误
这是组合查询的结果;并且请它强制我结合上面的查询,否则我将不得不更改我的整个代码逻辑,其次我已经使用$result = $stmt->execute ( $params ); 来执行查询,但它从我需要的SELECT 查询部分返回值使用$stmt->fetchAll();。还有其他方法吗?
另一种方式导致另一个问题
在其他无缓冲查询仍处于活动状态时无法执行查询
case PUT_COMMENTS :
$this->querymultiple ['insert'] = "INSERT INTO talks (face_id, comment, phone_id, created_at) VALUES (:fid, :com, :pid, :dat);";
$this->querymultiple ['update'] = "UPDATE facelog SET activity = activity + 1 WHERE face_id = :fid;
UPDATE facelog SET commentCount = commentCount + 1 WHERE face_id = :fid;";
$this->querymultiple ['select'] = "SELECT
talks.comment,
talks.face_id,
userlog.gcm_id
FROM
talks,
tagged,
userlog
WHERE
talks.face_id = :fid
AND talks.comment = :com
AND tagged.face_id = talks.face_id
AND userlog.phone_id = tagged.phone_id";
$this->params ['insert'] = array (
':' . FACE_ID => $value [FACE_ID],
':' . COMMENT => $value [COMMENT],
':' . DATE => $value [DATE],
':' . PHONE_ID => $value [PHONE_ID]
);
$this->params ['update'] = array (
':' . FACE_ID => $value [FACE_ID]
);
$this->params ['select'] = array (
':' . FACE_ID => $value [FACE_ID],
':' . COMMENT => $value [COMMENT]
);
$this->pdo_query->RunMultipleQuery ( $tag, $this->querymultiple, $this->params, SELECT );
break;
我的RunMultipleQuery()方法如下
/**
* This method runs multiple the Queries
*
* @param unknown $tag
* @param unknown $query
* @param unknown $params
* @param unknown $query_type
*/
public function RunMultipleQuery($tag, $query, $params, $query_type) {
try {
$stmt = array ();
$result = array ();
if (! empty ( $query ) || ! empty ( $params )) {
$this->pdo_db->pdo_db->beginTransaction ();
$stmt ['insert'] = $this->pdo_db->pdo_db->prepare ( $query ['insert'] );
$stmt ['update'] = $this->pdo_db->pdo_db->prepare ( $query ['update'] );
$stmt ['select'] = $this->pdo_db->pdo_db->prepare ( $query ['select'] );
$result ['insert'] = $stmt ['insert']->execute ( $params ['insert'] );
$result ['update'] = $stmt ['update']->execute ( $params ['update'] );
$result ['select'] = $stmt ['select']->execute ( $params ['select'] );
$stmt ['insert']->closeCursor ();
$stmt ['update']->closeCursor ();
$stmt ['select']->closeCursor ();
$this->pdo_db->pdo_db->commit ();
} else {
$result = null;
}
} catch ( PDOException $e ) {
// For testing, you could use a die and message.
// die("Failed to run query: " . $ex->getMessage());
// or just use this use this one to product JSON data:
// $response["message"] = '0:'.$ex->getMessage() ;
// die(json_encode($response["message"]));
$error_message = $e->getMessage ();
$error_code = $e->getCode ();
$error_trace = $e->getTrace ();
Log::LogError ( $error_message, $error_code, $error_trace );
// roll back transaction
if ($this->pdo_db->pdo_db->inTransaction ()) {
$this->pdo_db->pdo_db->rollBack ();
}
$result = null;
}
if ($query_type == SELECT) {
// echo 'result = '.$result;
try {
if (! empty ( $result ['select'] )) {
// fetching all the rows from the query
SendtoClient::Toclient ( $tag, $stmt ['select']->fetchAll () );
} else {
SendtoClient::Toclient ( $tag, null );
}
} catch ( PDOException $e ) {
$error_message = $e->getMessage ();
$error_code = $e->getCode ();
$error_trace = $e->getTrace ();
Log::LogError ( $error_message, $error_code, $error_trace );
}
}
}
【问题讨论】:
-
您的选择查询末尾没有分号,而且,您看过这篇文章吗? Multiple queries in PDO statement
-
我真的看不出为什么必须一起发送所有 3 个查询,也没有理由完全更改代码逻辑。这是您正在使用的愚蠢解决方案。只需启动一个事务,一个一个地发出所有查询,然后提交。从您的 POV 来看,加起来可能少于 50 个字符,但与您的示例不同 - 它会起作用。
-
是的,我看过@Osuwariboy 的帖子,但它并没有解决我的问题,我如何在没有
$stmt->fetchAll();或`$stmt->fetch();`的情况下获取我选择的值跨度> -
@N.B.我对您使用的“愚蠢”一词有点生气,但请在下面发布您的解决方案作为答案。
-
愚蠢这个词不是用来反映你的属性的。使用愚蠢这个词是因为解决方案很糟糕,这与你的技能、智商、能力或任何东西无关。我发表这样的评论只是为了反映这种解决方案在可能的解决方案中的价值——而且这种解决方案根本不好。如果你被愚蠢这个词冒犯了,那么我很抱歉,选择的解决方案很糟糕。我会发布一个答案来描述我的想法。