Php FOREACH和UPDATE mysql不工作

Php FOREACH和UPDATE mysql不工作,php,mysql,foreach,yii,sql-update,Php,Mysql,Foreach,Yii,Sql Update,我在foreach循环中有一个select查询,后跟一个update查询 我可以从select中获取数据,并将其用作json来绘制图表,但对于另一个应使用select中的相同数据进行更新的表,指定的字段将变为0,有时为1或2 我尝试使用一个select for update,它可以(update…(select…)但是在foreach内部它不起作用。 我还尝试使用其他列,但结果相同,总是得到0。 有什么想法吗? 非常感谢 这是我的密码 public function actionGetSenso

我在foreach循环中有一个select查询,后跟一个update查询

我可以从select中获取数据,并将其用作json来绘制图表,但对于另一个应使用select中的相同数据进行更新的表,指定的字段将变为0,有时为1或2
我尝试使用一个select for update,它可以(update…(select…)但是在foreach内部它不起作用。 我还尝试使用其他列,但结果相同,总是得到0。 有什么想法吗? 非常感谢

这是我的密码

public function actionGetSensorsDataLive($beamId) {
    header('Content-Type: application/json; charset="UTF-8"');
    $sensorsIds = Sensor::model()->findAllBySql('SELECT sensor_id FROM sensor where 
                             node_id="' . $beamId . '" and sensor_name = "I" ;');

    foreach ($sensorsIds as $s) {
        $sResult = array();
       $modelSensor = SensorInfo2::model()->findBySql('SELECT * FROM information_sensor  
             where sensor_id= "' . $s['sensor_id'] . '"');

        $sResult = array(($modelSensor->information_sensor_time),
            ($modelSensor->information_sensor_value));
        /////////////// update//////////////// 
        // for every information_sensor_time that I get from the previous query  
         //  I want
        //  to update a row in another table //

        foreach ($modelSensor as $up) {
            $connection = yii::app()->db;

            $sql = 'UPDATE last_point  SET last_point_time = "' . 
                             $up['information_sensor_time'] . '"
            WHERE sensor_id= "' . $s['sensor_id'] . '"  ';

            $command = $connection->createCommand($sql);
            $command->execute();
        }
        /////update end///////               
    }
    echo json_encode($sResult);
    Yii::app()->end();
}
删除

 $sensorsIds=Sensor::model()->findAllBySql('SELECT sensor_id FROM lead where 
                         node_id="'.$beamId.'" and sensor_name = "I"' );
现在试试这个。

您使用的是findBySql()`它只返回一条记录,然后对该记录执行foreach没有意义。所以,我选择了这条线

 $modelSensor = SensorInfo2::model()->findBySql('SELECT * FROM information_sensor  
             where sensor_id= "' . $s['sensor_id'] . '"');


您应该使用querybuilder,这是Alex建议的非常安全的

foreach ($sensors as $sensor)  {
$lastPointId = 1;  
$lastPoint = LastPoint::model()->findByPk($lastPointId);
$lastPoint->last_point_info = $sensor->information_time;
if ( ! $lastPoint->save()) {
    throw new CException('Unable to save last point.');
}
}

感谢所有Deang/Up>谢谢Deangon,同样的问题,我的代码的第一部分没有问题(因为我得到了数据,我可以画出来),这个问题在更新语句中,如果SQL是简单的,你应该考虑使用查询生成器,而不是直接插入参数。谢谢亚历克斯,我仍然有同样的问题。我认为我的问题在于foreach循环,以及如何在不使用循环的情况下将更新和选择合并在一起,因为我只有一条记录。有什么想法吗?谢谢,我想我应该解释得更好。这是一个场景,操作“actionGetSensorsDataLive”将通过ajax调用进行查询,以获取表中的最后一个条目,这就是我使用finBysql的原因。我想通过更新另一个表来保存提取点的记录。像这样,当我的表传感器信息实时更新时,我将修改我的查询以根据此记录获取点。我的问题是如何同时执行这两个查询select和update,以及在使用select查询的输出时是否可以生成select和update语句。foreach循环是否正确?许多人不理解这个问题。单个
传感器Info2
需要更新多少个点?关系是否有多个?当调用“actionGetSensorsDataLive”时,将只获取表中的最后一个点(findbysql)该点(x,y)我将使用它绘制一个图形,我想用值x更新表中的最后一个点,一秒钟后将进行另一个ajax调用(我的表不断更新)把我桌子上的最后一点拿出来,依此类推。每次获取(x,y)时,我都会用值x更新表的最后一个点。
$modelSensor = SensorInfo2::model()->findAllBySql('SELECT * FROM information_sensor  
             where sensor_id= "' . $s['sensor_id'] . '"');
foreach ($sensors as $sensor)  {
$lastPointId = 1;  
$lastPoint = LastPoint::model()->findByPk($lastPointId);
$lastPoint->last_point_info = $sensor->information_time;
if ( ! $lastPoint->save()) {
    throw new CException('Unable to save last point.');
}
}