Php yii1.3中sql语句的奇怪行为
我有一个控制器,我使用它的结果作为图像构造函数方法中图像的src,下面是代码:Php yii1.3中sql语句的奇怪行为,php,yii,Php,Yii,我有一个控制器,我使用它的结果作为图像构造函数方法中图像的src,下面是代码: static function getCamByClassIdForAdmin($id) { $connection=Yii::app()->db; $command=$connection->createCommand("Select Cam1,Cam2 from crd_classes where id={$id}");
static function getCamByClassIdForAdmin($id)
{
$connection=Yii::app()->db;
$command=$connection->createCommand("Select Cam1,Cam2 from crd_classes where id={$id}");
$camReader=$command->query();
$camList=$command->queryAll();
//I do such strange things to get result, because when I tried return $camList[0]['Cam1'] or to use indexes in constructor of image it says that it has undefined offset - 0 for some reason
$result="";
foreach ($camList as $cam)
{
foreach ($cam as $current => $currentValue)
{
if ($current=='Cam1')
{
$result=$currentValue;
}
}
}
$camId=$result;
return $result;
};
下面是我如何在图像构造函数中使用它:
'value' => function($data, $row) {
return CHtml::image(
//LessonsController::getCamByClassId($data->Lesson_ID)[0]['Cam1'],
LessonsController::getCamByClassIdForAdmin($data->Lesson_ID),
"default image",
array('width'=>195, 'height'=>110)
);
}
我想它会返回结果。接下来,我想在另一个查询中使用此查询的结果,所以我修改了如下控制器代码
static function getCamByClassIdForAdmin($id)
{
$connection=Yii::app()->db;
$command=$connection->createCommand("Select Cam1,Cam2 from crd_classes where id={$id}");
$camReader=$command->query();
$camList=$command->queryAll();
$result="";
foreach ($camList as $cam)
{
foreach ($cam as $current => $currentValue)
{
if ($current=='Cam1')
{
$result=$currentValue;
}
}
}
$camId=$result;
$connection1=Yii::app()->db;
$command1=$connection1->createCommand("Select cam from cam_names where id={$camId}");
$camReader1=$command1->query();
$camList1=$command1->queryAll();
return $result;
}
我收到的例外情况是:
The SQL statement executed was: Select cam from cam_names where id=
因此,$camId
似乎是空的,但它不是空的。为了检查这一点,我在$camId=$result之后添加了以下代码代码>
ob_start();
var_dump($camId);
$result=ob_get_clean();
但select语句仍然是空的。
你有什么建议为什么它会这样?
更新
我试过这个:
$result=$connection->createCommand("Select Cam1 from crd_classes where id={$id}")->queryScalar();
$resultCam=$connection->createCommand("Select cam from cam_names where id={$result}")->queryScalar();
这大大减少了代码。但错误是一样的
The SQL statement executed was: Select cam from cam_names where id=
更新1
我甚至试着把它们分成两种不同的方法
static function getCamByClassIdForAdmin($id)
{
$connection=Yii::app()->db;
$result=$connection->createCommand("Select Cam1 from crd_classes where id={$id}")->queryScalar();
return $result;
}
static function getCamNameByIdForAdmin($id)
{
$connection=Yii::app()->db;
$result=$connection->createCommand("Select cam from cam_names where id={$id}")->queryScalar();
return $result;
}
奇怪的行为仍然存在:
return CHtml::image(
LessonsController::getCamByClassIdForAdmin($data->Lesson_ID),
"default image",
array('width'=>195, 'height'=>110)
);
返回cam的id
但是
返回CHtml::image(
LessonsController::GetCamNameByIdorAdmin(LessonsController::GetCambyClassIdorAdmin($data->Lesson_ID)),
“默认图像”,
数组('width'=>195,'height'=>110)
);
说明错误:执行的SQL语句是:从cam_名称中选择cam,其中id=
我甚至试着把它们公开,而不是静态的。结果是一样的。
更新2
看起来,$id
未填充,但确实如此。证明:
static function getCamNameByIdForAdmin($id)
{
ob_start();
var_dump($id);
$result=ob_get_clean();
/*$connection=Yii::app()->db;
$result=$connection->createCommand("Select cam from cam_names where id={$id}")->queryScalar();*/
return $result;
}
返回:
string(3) "741
更新3
尝试了debug\u zval\u dump
而不是var\u dump
我收到
string(3) "741" refcount(3)
在我的src中,还有2个引用在函数之外。当我看到xdebug时,我希望能告诉你更多。这不是一个直接的答案,但为什么要使用queryal?crd_类中的“id”是否唯一?如果是这样,您可以使用$result=$connection->createCommand(“从crd_类中选择cam1,其中id={$id}”)->queryScalar();这应该只返回Cam1出现的一次。我稍后会检查您的答案。现在有另一个任务。@ChrisB我已更新了我的问题。因此,您的$id似乎没有填充,因为它没有显示在SQL中。确定尝试分解createcommand并查看您得到了什么1)$SQL=“从cam_名称中选择cam,其中id={$id}”;2) 变量转储($sql);3) $result=$connection->createCommand($sql)->queryScalar();