我的cli应用程序任务中有以下代码(类似于主应用程序中的控制器行为)。但是我发现有时当我执行这段代码时,我会得到如下错误。这表明SQL语句出了问题。但是,当我检查和测试时,即使它给我一个错误db更新成功。所以我想知道这个问题背后的根本原因是什么。
要执行的代码是
public function updateUserCountsAction(){
try{
$phql="UPDATE `user` u JOIN
(
SELECT COUNT(`user_to`) AS fav_count, user_to
FROM `users_favorite`
GROUP BY `user_to`
) uf
ON uf.user_to = u.user_id
SET u.fav_count = uf.fav_count;";
$user = new User();
$rs = new Phalcon\Mvc\Model\Resultset\Simple(null, $user, $user->getReadConnection()->query($phql));
}
catch(\Exception $e){
$this->error($e);
}
}
这是我有时会得到的错误,大概是50%的时间。我想知道为什么 ?但即使我收到此错误,SQL的执行似乎也会更新数据库。
========================================
2016-02-11 18:40:42:
PDOException: SQLSTATE[HY000]: General error
File=/var/www/vhosts/example.com/httpdocs/apps/cliend/tasks/CronTask.php
Line=240
#0 [internal function]: PDOStatement->fetchAll()
#1 [internal function]: Phalcon\Db\Result\Pdo->fetchAll()
#2 [internal function]: Phalcon\Mvc\Model\Resultset->__construct(Object(Phalcon\Db\Result\Pdo), NULL)
#3 /var/www/vhosts/example.com/httpdocs/apps/cliend/tasks/CronTask.php(240): Phalcon\Mvc\Model\Resultset\Simple->__construct(NULL, Object(LXY\Models\User), Object(Phalcon\Db\Result\Pdo))
#4 [internal function]: CronTask->updateUserCountsAction()
#5 [internal function]: Phalcon\Dispatcher->dispatch()
#6 /var/www/vhosts/example.com/httpdocs/apps/cliend/console.php(94): Phalcon\Cli\Console->handle(Array)
#7 {main}
答案 0 :(得分:2)
我会责怪你的mysql_nd配置只有50%的错误率,但首先,尝试不使用Resultset。更新查询不会返回值(1,2):
getWriteConnection()
在您实际尝试写入数据库时也使用HY000
。
如果两者都没有帮助,请检查您的mysql_nd配置。在我的群集中配置了死对象时,发现 -(void)callSegmentSelected
{
value1 = (int)segment.selectedSegmentIndex;
if (segment.selectedSegmentIndex == 0)
{
NSString *urlString = [NSString stringWithFormat:@"http:"url"];
NSString *jsonString = [NSString stringWithFormat:catid];
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if(str.length > 0)
{
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
array =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[myTableView reloadData];
}
else
{
NSLog(@"Error");
}
}
else if (segment.selectedSegmentIndex == 1)
{
}
错误并正确执行了脚本。