পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, March 22, 2017

Yii - Data Access Objects

To execute an SQL query, you should follow these steps −
  • Create an yii\db\Command with an SQL query.
  • Bind parameters (not required)
  • Execute the command.
Step 1 − Create a function called actionTestDb in the SiteController.
public function actionTestDb(){
   // return a set of rows. each row is an associative array of column names and values.
   // an empty array is returned if the query returned no results
   $users = Yii::$app->db->createCommand('SELECT * FROM user LIMIT 5')
      ->queryAll();
   var_dump($users);
   // return a single row (the first row)
   // false is returned if the query has no result
   $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE id=1')
      ->queryOne();
   var_dump($user);
   // return a single column (the first column)
   // an empty array is returned if the query returned no results
   $userName = Yii::$app->db->createCommand('SELECT name FROM user')
      ->queryColumn();
   var_dump($userName);
   // return a scalar value
   // false is returned if the query has no result
   $count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM user')
      ->queryScalar();
   var_dump($count);
}
The above example shows various ways of fetching data from a DB.
Step 2 − Go to the address http://localhost:8080/index.php?r=site/test-db, you will see the following output.
Create actionTestDb Output

Create an SQL Command

To create an SQL command with parameters, you should always use the approach of binding parameters to prevent the SQL injection.
Step 1 − Modify the actionTestDb method this way.
public function actionTestDb() {
   $firstUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE id = :id')
      ->bindValue(':id', 1)
      ->queryOne();
   var_dump($firstUser);
   $params = [':id' => 2, ':name' => 'User2'];
   $secondUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE
      id = :id AND name = :name')
      ->bindValues($params)
      ->queryOne();
   var_dump($secondUser);
      //another approach
   $params = [':id' => 3, ':name' => 'User3'];
   $thirdUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE
      id = :id AND name = :name', $params)
      ->queryOne();
   var_dump($thirdUser);
}
In the code above −
  • bindValue() − binds a single parameter value.
  • bindValues() − binds multiple parameter values.
Step 2 − If you go to the address http://localhost:8080/index.php?r=site/test-db, you will see the following output.
Modified actionTestDb Output

INSERT, UPDATE and DELETE Queries

For INSERT, UPDATE, and DELETE queries, you may call insert(), update(), and delete() methods.
Step 1 − Modify the actionTestDb method this way.
public function actionTestDb() {
   public function actionTestDb(){
      // INSERT (table name, column values)
      Yii::$app->db->createCommand()->insert('user', [
         'name' => 'My New User',
         'email' => 'mynewuser@gmail.com',
      ])->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User')
         ->queryOne();
      var_dump($user);
      // UPDATE (table name, column values, condition)
      Yii::$app->db->createCommand()->update('user', ['name' => 'My New User
         Updated'], 'name = "My New User"')->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User Updated')
         ->queryOne();
      var_dump($user);
      // DELETE (table name, condition)
      Yii::$app->db->createCommand()->delete('user', 'name = "My New User
         Updated"')->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User Updated')
         ->queryOne();
      var_dump($user);
   }
}
Step 2 − Type the URL http://localhost:8080/index.php?r=site/test-db in the address bar of the web browser and you will see the following output.
Insert Update Delete Queries Example

No comments:

Post a Comment