Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查询如何在Yii2中使用_Yii_Yii2_Yii2 Advanced App - Fatal编程技术网

查询如何在Yii2中使用

查询如何在Yii2中使用,yii,yii2,yii2-advanced-app,Yii,Yii2,Yii2 Advanced App,你能帮我吗。我想喜欢,但在我的源代码上它变成了空的。我的项目中的查询或源代码是什么?多谢各位 内部控制器 public function actionView($id) { $con = Yii::$app->db; $sql = $con->createCommand("SELECT * FROM track where collecting_id=$id ORDER BY collecting_id desc"); $posts = $sql->qu

你能帮我吗。我想喜欢,但在我的源代码上它变成了空的。我的项目中的查询或源代码是什么?多谢各位

内部控制器

public function actionView($id)
{
    $con = Yii::$app->db;
    $sql = $con->createCommand("SELECT * FROM track where collecting_id=$id ORDER BY collecting_id desc");
    $posts = $sql->query();
    return $this->render('view', [
        'model' => $this->findModel($id),
        'posts' => $posts,
    ]);
}
鉴于

<div class="timeline__items">
<?php
foreach($posts as $row)
{
?>
<div class="timeline__item">
   <div class="timeline__content">
   <h2><?php echo $row['status']; ?></h2>
   <p><?php echo $row['tanggal']; ?></p>
</div>
</div>
<?php
}
?>
</div>

结果是。

在将列与用户提供的或用户可以编辑的任何此类输入进行比较时,绑定参数总是很好的,就像您将作为参数传递给
actionView()
$id
一样。然后需要使用
queryAll()
queryOne()
以防返回多行或单行

因此,您应该将查询更改为以下内容

public function actionView($id)
{
    $con = Yii::$app->db;
    $sql = $con->createCommand(
        "SELECT * FROM track where collecting_id=:collecting_id ORDER BY collecting_id desc",
        [':collecting_id' => $id]
    );
    $posts = $sql->queryAll();
    return $this->render(
        'view', [
            'model' => $this->findModel($id),
            'posts' => $posts,
        ]
    );
}


除上述内容外,还应使用ActiveRecord。Active Record提供了一个面向对象的接口,用于访问和操作存储在数据库中的数据。阅读使您的生活更轻松。

在将列与用户提供的或用户可以编辑的任何此类输入进行比较时,绑定参数总是很好的,就像您将作为参数传递给
actionView()
$id
一样。然后需要使用
queryAll()
queryOne()
以防返回多行或单行

因此,您应该将查询更改为以下内容

public function actionView($id)
{
    $con = Yii::$app->db;
    $sql = $con->createCommand(
        "SELECT * FROM track where collecting_id=:collecting_id ORDER BY collecting_id desc",
        [':collecting_id' => $id]
    );
    $posts = $sql->queryAll();
    return $this->render(
        'view', [
            'model' => $this->findModel($id),
            'posts' => $posts,
        ]
    );
}


除上述内容外,还应使用ActiveRecord。Active Record提供了一个面向对象的接口,用于访问和操作存储在数据库中的数据。阅读让你的生活更轻松。

$posts=$sql->queryAll();要检查您收到的退货,请打印($posts);是否有特殊原因不使用
ActiveRecord
hereresult。数组()1@tigrastibut我使用代码$sql=$con->createCommand(“通过收集_id desc从收集_id='PMUEI'订单的轨迹中选择*);这是工作。但是如果use$id变为空。如何使用?对不起,我不知道如何在我的代码@MuhammadOmerAslam$posts=$sql->queryAll()中使用activerecord;要检查您收到的退货,请打印($posts);是否有特殊原因不使用
ActiveRecord
hereresult。数组()1@tigrastibut我使用代码$sql=$con->createCommand(“通过收集_id desc从收集_id='PMUEI'订单的轨迹中选择*);这是工作。但是如果use$id变为空。如何使用?对不起,我不知道如何在我的代码@MuhammadOmerAslamit的工作中使用activerecord。谢谢$posts=Track::find()->其中(['collecting_id'=>$model->collection_id])->全部();这是使用ActiveRecord的工作。谢谢$posts=Track::find()->其中(['collecting_id'=>$model->collection_id])->全部();